Casting UIWindow.rootView to UIHostingController<View> which has environment variables

1.1k Views Asked by At

Casting UIWindow.rootView to a hosting controller like:

window?.rootViewController as? UIHostingController<MainTabView>

works fine, unless it has environmentObjects.

As my MainTabView has been initialized with .environmentObject, I cannot cast it to UIHostingController<MainTabView>.

When I try to print rootView's type, Xcode tells:

<_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentGS1_V10Panparella11MainTabViewGVS_30_EnvironmentKeyWritingModifierGSqCS2_11GroupsStore___GS4_GSqCS2_18GroupsPresentation____: 0x7fd8afe5da40>

which I suppose is:

UIHostingController<ModifiedContent<MainTabView, _EnvironmentKeyWritingModifier<GroupsStore, GroupsPresentation>>>

but I'm not able to reach _EnvironmentKeyWritingModifier.

Anybody has an idea?

1

There are 1 best solutions below

0
On

If there is only one EnvironmentObject the following typecast compiles and works fine.

UIHostingController<ModifiedContent<MainTabView, _EnvironmentKeyWritingModifier<Optional<GroupStore>>>>

Even though this is not a complete answer to the question (since there are multiple EnvironmentObjects) I am posting it here in case it might help someone that uses a single EnvironmentObject.