Szabolcs Toth
Szabolcs Toth

Reputation: 63

@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"

Is there any way to initialize and use variable in the same view? Thank you!

Upvotes: 0

Views: 762

Answers (1)

Isaaс Weisberg
Isaaс Weisberg

Reputation: 2824

fetchSingleCompetitionData is being initialized as part of init. Lazy vars are only available afterwards. Consider moving the initialization to a fully-qualified initializer.

Upvotes: 2

Related Questions