let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
// Add `@Environment(\.managedObjectContext)` in the views that will need the context.
let loginView = LoginView().environmentObject(GlobalEnviroment())
loginView.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: loginView)
self.window = window
window.makeKeyAndVisible()
}
}
1条答案
按热度按时间ikfrs5lh1#
.environment
返回一个修改过的View
版本,它传递了环境。你需要实际保留/使用该函数的结果。最简单的方法就是把它链接到你已经拥有的东西上。