Monday, October 23, 2023
HomeiOS Developmentios - Why I can not conform to a protocol with an...

ios – Why I can not conform to a protocol with an @EnvironmentObject variable?


So I am making an attempt to adapt to a protocol utilizing an @EnvironmentObject variable, however the compiler complains, whereas if a use @ObservedObject it really works.

Utilizing @EnvironmentObject:

ultimate class ViewModel: ObservableObject {
    @Revealed var someOutput: String

    init(someOutput: String) {
        self.someOutput = someOutput
    }
}

protocol SomeChildView: View {
    var viewModel: ViewModel { get set }
}

struct FirstChildView: SomeChildView {
    @EnvironmentObject var viewModel: ViewModel // <-- This doesn't work.

    var physique: some View {
        Textual content("")
    }
}

Utilizing @ObservedObject:

struct FirstChildView: SomeChildView {
    @ObservedObject var viewModel: ViewModel // <-- Works

    var physique: some View {
        Textual content("")
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments