Lazy Stored Properties--无括号时为匿名函数
第一次使用的时候进行计算和初始化,后面的引用不在进行计算。
A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy
modifier before its declaration.
NOTE
You must always declare a lazy property as a variable (with the var
keyword), because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.
Lazy properties are useful when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete. Lazy properties are also useful when the initial value for a property requires complex or computationally expensive setup that should not be performed unless or until it is needed.
class Lazytest{
var xx = 1
lazy var ee:String = {
return "\(self.xx)"
}()
lazy var ff = {
return "\(self.xx)"
}
lazy var gg = {para in
return para + "\(self.xx)"
}
}
有无括号的区别。无括号时为匿名函数
ee.storage String? "1" some
ff.storage (() -> String)? nil none
gg.storage ((String) -> String)? nil none
(lldb) po px.ff()
"4"
(lldb) po px.gg("aa")
"aa4"
Lazy Stored Properties--无括号时为匿名函数的更多相关文章
- C#绑定事件时使用匿名函数
当使用一些临时的函数 可以预知这些函数基本不会被复用时 可以使用匿名函数简化代码 public static void startCoupons() { //绑定一些事件 userGetCoupon ...
- 无参数的lambda匿名函数
lambda 语法: lambda [arg1[,arg2,arg3....argN]]:expression 1.单个参数的: g = lambda x:x*2 print g(3) 结果是6 2. ...
- javascript 匿名函数的理解,js括号中括function 如(function(){})
代码如下: (function(){ //这里忽略jQuery所有实现 })(); (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也 ...
- 自执行的匿名函数!function()
最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2个月前我回杭州最后参加团队会议的时候,@西子剑影抛出的一样的问题:如果在function之前加上感叹号 (!) 会 ...
- JS自执行匿名函数
常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号内为匿 ...
- jquery的匿名函数研究
jQuery片段: ? 1 2 3 ( function (){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在 ...
- [从jQuery看JavaScript]-匿名函数与闭包(Anonymous Function and Closure)【转】
(function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的第一眼,我就迷糊了.为什么只有 ...
- JS 匿名函数 自执行
其实就是将函数直接做为表达调用,使用括号包裹定义函数体,解析器将会以函数表达式的方式去调用定义函数. 常见格式:(function() { /* code */ })(); 解释:包围函数(funct ...
- python匿名函数
文章导读: 以前自己一直没搞明白Python中的匿名函数,现在拿这个问题基本上搞明白了,拿自己的理解整成一篇文章,附带大量例子,让其更加好理解. 在编程语言中,函数的应用: 1. 代码块重复,这时候必 ...
随机推荐
- PHP统计当前在线用户数实例
HTML 我们在页面上放置一个显示当前在线人数的div#total以及一个用于展示访客地区分布的列表#onlinelist,默认我们在列表中放置一张与加载动画图片,后面我们用jQuery控制当鼠标滑向 ...
- mysql中explain用法和结果的含义
explain select * from user explain select * from user explain extended select * from user explain e ...
- 【ACM】poj_1363_Rails_201308081502
Rails Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21528 Accepted: 8597 Descriptio ...
- asp.net--ado.net5大对象代码示例
连接数据库 string conn_string ="Data Source=localhost;Initial Catalog=SQLtest;Integrated Security=Tr ...
- asp.net--常用的数据库链接字符串
本地连接 privatestring conn_string ="Data Source=localhost;Initial Catalog=SQLtest;Integrated Secur ...
- 程序错误通常有2种,一种是运行时错误,这种错误ide会抛出异常,我们可以根据异常寻找问题.
1.程序错误通常有2种,一种是运行时错误,这种错误ide会抛出异常,我们可以根据异常寻找问题. 2.可以根据异常寻找问题. 3.可以根据异常寻找问题
- Leetcode:remove_element
一. 题目 给定一个数组和一个值.删除当中和给定值相等的元素.返回得到的新数组长度 二. 分析 刚開始我以为仅仅须要返回最后的数组长度即可了呢! 后来WA了一次才知道还得把心数组构造好 ...
- HDU 5218 The E-pang Palace (简单几何—2014广州现场赛)
题目链接:pid=5128">http://acm.hdu.edu.cn/showproblem.php? pid=5128 题面: The E-pang Palace Time Li ...
- 修改linux内核开机logo并居中全屏显示【转】
本文转载自:http://blog.csdn.net/xuezhimeng2010/article/details/49299781 1.准备图片 使用ubuntu自带的绘图软件GIMP是最为快捷的 ...
- 【NOIP 2011】 计算系数
[题目链接] https://www.luogu.org/problemnew/show/P1313 [算法] 二项式定理 [代码] #include<bits/stdc++.h> usi ...