gpt4 book ai didi

swift - 在 Swift 中,我们需要初始化惰性存储属性吗?

转载 作者:可可西里 更新时间:2023-11-01 00:38:25 32 4
gpt4 key购买 nike

在 Apple 的新 Swift 编程语言中,我遇到了以下情况:

类和结构必须在创建该类或结构的实例时将其所有存储属性设置为适当的初始值。存储属性不能处于不确定状态。

上述规则是否对@lazy 存储属性有效?

最佳答案

不,你没有:

A lazy stored property is a property whose initial value is not calculated until the first time it is used...

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...

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.

Source

基本上意味着它没有值并且在初始化后不需要立即值。

关于swift - 在 Swift 中,我们需要初始化惰性存储属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24061738/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com