gpt4 book ai didi

Swift 初始化惰性值

转载 作者:行者123 更新时间:2023-11-28 07:53:31 24 4
gpt4 key购买 nike

我有课

struct Word {
let text : String
let added: Date
let willShow: Date
let folder: Phase
let bestTranslation: String
let translationsProvider: ()->[String]
lazy var allTranslations : [String] = {self.translationsProvider()}()
}

和初始化器

extension Word {
init(_ definition: Definition, context translationProvider:
@escaping ()->[String]){
self.text = definition.text!
self.added = definition.added!
self.willShow = definition.willShow!
self.folder = Phase(rawValue: definition.folder)!
self.bestTranslation = definition.best?.text ?? ""
self.translationsProvider = translationProvider
}
}

我遇到了错误

Return from initializer without initializing all stored properties

有描述

  1. 'self.allTranslations.storage' not initialized

我该如何解决?

最佳答案

我想知道这是否可能是编译器错误。我没有看到你得到的相同错误,但这将创建 Swift 编译器段错误 11:

struct Test {
let foo: String
lazy var bar: String = { return "" }()
}

extension Test {
init(_ foo: String) {
self.foo = foo
}
}

关于Swift 初始化惰性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999043/

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