3

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?

Rashid
  • 1,515
  • 16
  • 16
  • What gives you casting to `UIHostingController`? ... anyway, just wrap it in your custom view controller w/o generics... bug casting is bad... very bad. ;) – Asperi Feb 14 '21 at 14:57
  • it's nil when casted to `UIHostingController` and I can't wrap it without giving UIHostingController's rootView type. As it's not my custom view controller, it doesn't seem possible to reach its view without casting it. – Rashid Feb 14 '21 at 17:36

1 Answers1

0

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.

DCDC
  • 486
  • 5
  • 9