Post

Replies

Boosts

Views

Activity

Reply to How to create UIHostingController and pass environmentObject?
UPDATE: Ended up creating a wrapper view MyView_RUNTIME which has the runtime dependencies specified. This way in the #preview block you can instantiate MyView with your dummy environment variables, while in the UIHostingController you can instantiate the real ones. ie: // use within UIHostingController (ie: super.init( coder: aDecoder, rootView: DebugView_RUNTIME() )) struct MyView_RUNTIME : View { var body: some View { MyView().environment( \.database, ServiceLocator.database ) // NOTE: real DB registered during the bootstrapping of the runtime app } } // use within previews #Preview { MyView().environment( \.database, MyDatabase( inMemory: true )) // in-memory, dummy database }
Oct ’23