简体   繁体   English

@ObservedObject 带有属性初始化器 - SwiftUI

[英]@ObservedObject with property initializer - SwiftUI

I have this code:我有这个代码:

struct TestView: View {

    lazy var currentCompetitionID: String

    @ObservedObject var fetchSingleCompetitionData = FetchSingleCompetitionData(option: "\(currentCompetitionID)")

    var body: some View {
       Text("\(fetchSingleCompetitionData.competitionList.name)")
    }
}

I receive the error: "Cannot use instance member 'currentCompetitionID' within property initializer; property initializers run before 'self' is available"我收到错误消息:“无法在属性初始化程序中使用实例成员 'currentCompetitionID';属性初始化程序在 'self' 可用之前运行”

Is there any way to initialize and use variable in the same view?有没有办法在同一个视图中初始化和使用变量? Thank you!谢谢!

fetchSingleCompetitionData is being initialized as part of init . fetchSingleCompetitionData正在作为 init 的一部分进行init Lazy vars are only available afterwards.惰性变量仅在之后可用。 Consider moving the initialization to a fully-qualified initializer.考虑将初始化移动到完全限定的初始化器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM