关键字是lazy,只能是var的变量才能懒加载
lazy var count = 100
lazy var count = {
return 100
}()
//闭包中如果是多行代码就要报错:unable to infer complex closure return type
lazy var count = {
var a = 100
var b = 100
return a+b
}()
lazy var count: Int = {
var a = 100
var b = 100
return a+b
}()
lazy var count = {
var a = 100
var b = 100
return a+b
}() as Int
lazy var count = {
() -> Int in
var a = 100
var b = 100
return a+b
}()
原文链接:https://www.f2er.com/swift/321201.html