使用 SceneDelegate 设置 CoreData - 未知标识符 'window' 错误 - iOS 13 起
Setting Up CoreData with SceneDelegate - unknown identifier 'window' error - iOS 13 onwards
我试图使用 Core Data 的官方苹果文档。找到 here. I also ran into a question which was related to my issue, right here on .
我 运行 遇到一个问题,它一直说 'window' 在 AppDelegate 的上下文中不可用。根据官方文档,这是非常基本的步骤。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let rootVC = window?.rootViewController as? ViewController {
rootVC.container = persistentContainer
}
return true
}
我该如何克服这个问题?
问题归结为更改,主要是 iOS 13 及更高版本中对多个场景的支持。检查这个 reddit link for the discussion.
解决方案是将一些东西从 AppDelegate 移动到 SceneDelegate。
这里是上面两个重要部分的最终形式类。
----场景代理
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let rootVC = storyboard.instantiateViewController(identifier: "ViewController") as? ViewController else {
print("ViewController not found")
return
}
//set the storage here
rootVC.container = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer
//I dont want a UI navigation controller.
//let rootNC = UINavigationController(rootViewController: rootVC)
//self.window?.rootViewController = rootNC
//I want to use my basic view controller here. use rootNC to get a UI navigation controller
self.window?.rootViewController = rootVC
self.window?.makeKeyAndVisible()
}
--AppDelegate(保持不变)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//before iOS 13 you would be putting stuff here but not anymore.
return true
}
最后,您将在 AppDelegate 本身中保留与存储相关的代码。
我试图使用 Core Data 的官方苹果文档。找到 here. I also ran into a question which was related to my issue, right here on
我 运行 遇到一个问题,它一直说 'window' 在 AppDelegate 的上下文中不可用。根据官方文档,这是非常基本的步骤。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let rootVC = window?.rootViewController as? ViewController {
rootVC.container = persistentContainer
}
return true
}
我该如何克服这个问题?
问题归结为更改,主要是 iOS 13 及更高版本中对多个场景的支持。检查这个 reddit link for the discussion.
解决方案是将一些东西从 AppDelegate 移动到 SceneDelegate。
这里是上面两个重要部分的最终形式类。
----场景代理
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let rootVC = storyboard.instantiateViewController(identifier: "ViewController") as? ViewController else {
print("ViewController not found")
return
}
//set the storage here
rootVC.container = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer
//I dont want a UI navigation controller.
//let rootNC = UINavigationController(rootViewController: rootVC)
//self.window?.rootViewController = rootNC
//I want to use my basic view controller here. use rootNC to get a UI navigation controller
self.window?.rootViewController = rootVC
self.window?.makeKeyAndVisible()
}
--AppDelegate(保持不变)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//before iOS 13 you would be putting stuff here but not anymore.
return true
}
最后,您将在 AppDelegate 本身中保留与存储相关的代码。