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"
相关文章
- 教程-Delphi源代码--后延函数
- 缓动函数与关键帧动画
- Swift基础语法之变量函数
- 视觉智能开放平台通过函数计算实现多人口罩佩戴识别
- 函数计算自动化运维实战 3 -- 事件触发自动创建快照
- 函数计算自动化运维实战 2 -- 事件触发 eip 自动转移
- BAT 批处理 常用功能 工具 函数 补充 [MD]
- (剑指Offer)面试题1:赋值运算符函数
- Paip.断点调试MYSQL存储过程跟函数的解决方案大法
- mysql常用基础操作语法(七)--统计函数和分组查询【命令行模式】
- Python 自带的 any() 函数--判断给定的可迭代参数 iterable 是否全部为 False
- SQLServer charindex函数, 查 某个字符 或 某个字符串 在 另一个字符串中的位置
- 97.第十九章 MySQL数据库 -- MySQL视图、函数、触发器及用户权限管理(七)
- 6.3 cmath--数学函数
- 8.4 stat--stat()函数的结果处理
- python: numpy--函数 shape用法
- asp.net core 3.1 入口:Program.cs中的Main函数
- 【Python】实现MATLAB中计算两个矩形相交面积的rectint函数
- C++基础知识要点--函数(Primer C++ 第五版 · 阅读笔记)