I want to inject an EnvironmentObject while creating a SwiftUI view in watchOS6.
But since WKHostingController expects a Concrete type I am not able to do the following ContentView().environmentObject(UserData())
class HostingController: WKHostingController<ContentView> {
override var body: ContentView {
return ContentView().environmentObject(UserData())
}
}
This code fails with the following error:
Cannot convert return expression of type 'some View' to return type 'ContentView'
I have seen workaround like this : Using environmentObject in watchOS this seems like a hack instead of a proper solution.
I asked a watchOS engineer on Twitter regarding the same and his reply was to place .environmentObject(UserData())
inside the body of ContentView()
. I tried doing that but Xcode reports an error.
So has anyone found a way to do the same ?