0

如何在 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()
        }

谢谢你的帮助!

4

1 回答 1

0

这对我来说很好。

let userSettings = UserSettings()

let contentView = ContentView()
        .environment(\.managedObjectContext, context)
        .environmentObject(userSettings)
于 2020-04-28T06:56:54.103 回答