使用SceneDelegate设置CoreData-未知标识符“窗口"错误-iOS 13及更高版本 [英] Setting Up CoreData with SceneDelegate - unknown identifier 'window' error - iOS 13 onwards

查看:114
本文介绍了使用SceneDelegate设置CoreData-未知标识符“窗口"错误-iOS 13及更高版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apple官方数据作为Core Data.在此处找到.我也遇到了一个与我的问题相关的问题,就在这里

I was trying to use the official apple documentation for Core Data. Found here. I also ran into a question which was related to my issue, right here on stack.

我遇到一个问题,它一直说在AppDelegate的上下文中窗口"不可用.根据官方文档,这是非常基本的步骤.

I ran into an issue where, it kept saying that 'window' is not available in the context of AppDelegate. This is the very basic step as per the official documentation.

    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链接进行讨论.

The issue boils down to the changes, primarily, support for multiple scenes in iOS 13 and above. check this reddit link for the discussion.

解决方案是将某些东西从AppDelegate移到SceneDelegate.

The solution is to move some of things from AppDelegate to SceneDelegate.

这是以上两个类别中必不可少的部分的最终形式.

Here is the final form of the essential parts of the above two classes.

---- SceneDelegate

----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(保持不变)

--AppDelegate (remains unchanged )

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本身中.

Finally, you will leave the storage related code, where it was, in AppDelegate itself.

这篇关于使用SceneDelegate设置CoreData-未知标识符“窗口"错误-iOS 13及更高版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆