gpt4 book ai didi

swift - willSet 问题中的延迟初始化

转载 作者:行者123 更新时间:2023-11-28 15:29:43 27 4
gpt4 key购买 nike

我正在尝试创建可选数组,但我希望仅在将元素附加到之前进行初始化。

所以我写道:

    var names: [String]? {
willSet {
if names == nil {
names = []
}
}
}

但是我得到这个错误:

Attempting to store to property 'names' within its own willSet, which is about to be overwritten by the new value

最佳答案

如果你想要一个懒惰的酒吧,那么你应该这样做......

lazy var names: [String] = []

关于swift - willSet 问题中的延迟初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44859456/

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