Deitsch
Deitsch

Reputation: 2178

Add EnvironmentObject in SwiftUI 2.0

Since SwiftUI 2.0 does not have an AppDelegate and SceneDelegate anymore, where should EnvironmentObjects be set?

This how it was done previously, where do i have to add them now?

window.rootViewController = UIHostingController(rootView: ContentView()
                             .environmentObject(settings))

Upvotes: 0

Views: 1143

Answers (1)

pawello2222
pawello2222

Reputation: 54486

Try the following:

@main
struct TestApp: App {
    @StateObject var settings: Settings = ... // init here

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(settings)
        }
    }
}

Upvotes: 5

Related Questions