Lazy Stored Properties--无括号时为匿名函数

第一次使用的时候进行计算和初始化,后面的引用不在进行计算。

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"

 

转载于:https://www.cnblogs.com/feng9exe/p/9141517.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值