debugging 如何解决“调用环境的结果未使用”的错误?

zsbz8rwp  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(125)
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()
}
}
ikfrs5lh

ikfrs5lh1#

.environment返回一个修改过的View版本,它传递了环境。你需要实际保留/使用该函数的结果。
最简单的方法就是把它链接到你已经拥有的东西上。

let loginView = LoginView()
   .environmentObject(GlobalEnviroment())
   .environment(\.managedObjectContext, context)

if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: loginView)
        self.window = window
        window.makeKeyAndVisible()
}

相关问题