Document
https://developer.apple.com/documentation/coredata/setting_up_a_core_data_stack
Version
> xcodebuild -version
Xcode 13.1
Build version 13A1030d
Issue
class AppDelegate: UIResponder, UIApplicationDelegate {
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// ERROR: Cannot find 'window' in scope
if let rootVC = window?.rootViewController as? ViewController {
rootVC.container = persistentContainer
}
return true
}
}
Error message: "Cannot find 'window' in scope" I get this error and can't build. What should I do?
Should I create the window myself?
Normally, you do this now in SceneDelegate.
But if you want to do it as well in AppDelegate, you need to declare:
var window: UIWindow? // For iOS 12 and below
In SceneDelegate, you should have:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
@available(iOS 13.0, *)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
}