ios - Swift 中的惰性属性等同于 Objective C 中的惰性 Init getter

标签 ios objective-c swift lazy-evaluation

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/

相关文章:

ios - 从导航 Controller 中的滚动 UIScrollview 向后导航后,我的应用程序崩溃

objective-c - 语音开始时自动录音,语音停止时自动播放

ios - 具有两个标识符的表格 View 单元格。对于 TableView 中的奇数一个单元格和甚至另一个单元格

iOS 核心数据迁移大问题

ios - 延迟背景音乐开始

ios - iPhone6 和 iPhone 6 Plus 的设备宽度 css 视口(viewport)尺寸是多少

ios - 为 Alamofire 请求输入用户名和密码

iOS - 以编程方式更改颜色 channel 级别

iOS 启用 kAudioUnitProperty_MeteringMode

ios - 当应用程序终止时,位置后台模式是否可以快速工作?