有没有办法在 SceneDelegate 中初始化 CoreData 和 EnvironmentObject?

Is there a way to Init CoreData AND EnvironmentObject in SceneDelegate?

如何在 scenedelegate 根视图中初始化 managedObjectContext 和 .environmentObject?

我尝试了这段代码的所有变体 - 它没有用:

let contentView = ContentView().environment(\.managedObjectContext, context)

        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView.environmentObject(UserSettings()))
            self.window = window
            window.makeKeyAndVisible()
        }

感谢您的帮助!

这对我来说效果很好。

let userSettings = UserSettings()

let contentView = ContentView()
        .environment(\.managedObjectContext, context)
        .environmentObject(userSettings)