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)) 回答1: Try the following: @main struct TestApp: App { @StateObject var settings: Settings = ... // init here var body: some Scene { WindowGroup { ContentView() .environmentObject(settings) } } } 来源: https:/