Swift 中的 lazy 属性是否等同于在 Objective C 中使用延迟加载模式覆盖 getter?
最佳答案
来自文档:
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 attribute before its declaration.
所以,大多数情况下,是的。
You must always declare a lazy property as a variable (with the var keyword), because its initial value may 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.”
请记住,在 Swift 上,您可以选择为您的属性声明自定义 getter 和 setter:
var name : String?{
get{
return "Oscar"
}
set(newValue){
}
}
关于ios - Swift 中的惰性属性等同于 Objective C 中的惰性 Init getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24023664/