DEV Community

Narongdej Sarnsuwan
Narongdej Sarnsuwan

Posted on • Originally published at narongdej.dev

1

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.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay