DEV Community

Narongdej Sarnsuwan
Narongdej Sarnsuwan

Posted on • Originally published at narongdej.dev

Missing 'window' variable in AppDelegate - CoreData

If you're following along the "Setting Up a Core Data Stack" tutorial for Swift in apple website, and you got stuck on the rootVC = window?.rootViewController part, then you're not alone.

Since iOS 13 (Xcode 11) the life cycle of UIKit introduce UISceneDelegate, read more here.

Thus, you have to change use the following code instead of in the tutorial

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate {

    ...

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {        
        // Just leave it as it, don't change anything
    }

    ...
}
Enter fullscreen mode Exit fullscreen mode

SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        ...

    rootVC.container = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer

        ...

}
Enter fullscreen mode Exit fullscreen mode

It sure confuse the hell out of me, when I just getting start developing iOS app and following the tutorial.

Top comments (0)