Theo Conrad
Theo Conrad

Reputation: 31

Is 'window' not an identifier in Swift iOS Development?

I am creating an iOS application. I have run into a problem: the identifier 'window' is unresolved.

This may be because I'm using the beta version of Swift and beta version of Xcode, but I need them to develop this app. I have tried searching other questions but none answer about the identifier 'window'.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    guard let splitViewController = window?.rootViewController as? UISplitViewController,
        let leftNavController = splitViewController.viewControllers.first as? UINavigationController,
        let masterViewController = leftNavController.topViewController as? MasterViewController,
        let detailViewController = splitViewController.viewControllers.last as? DetailViewController
        else { fatalError() }

    let firstMonster = masterViewController.monsters.first
    detailViewController.monster = firstMonster

    return true
}

I expect to be able to, in the end, select a menu item and have details show up on another View Controller, but I am getting an error with an unresolved identifier.

Upvotes: 2

Views: 1856

Answers (1)

matt
matt

Reputation: 534893

This may be because I'm using the beta version of Swift and beta version of Xcode

In iOS 13, and therefore in any new project created under Xcode 11, the window property is moved out of the application delegate and now resides in the scene delegate.

https://developer.apple.com/documentation/uikit/uiscenedelegate

The window property is not formally part of the UISceneDelegate protocol, but it is part of the UIWindowSceneDelegate protocol:

https://developer.apple.com/documentation/uikit/uiwindowscenedelegate

In the new architecture, the window scene delegate instance is the locus of interface creation and maintenance, including access to the window, configuration of its rootViewController, and so on. The project template now includes a file SceneDelegate.swift, containing the declaration for SceneDelegate class which conforms to UIWindowSceneDelegate; here is the structure of the boilerplate code:

import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
    }
    func sceneDidDisconnect(_ scene: UIScene) {
    }
    func sceneDidBecomeActive(_ scene: UIScene) {
    }
    func sceneWillResignActive(_ scene: UIScene) {
    }
    func sceneWillEnterForeground(_ scene: UIScene) {
    }
    func sceneDidEnterBackground(_ scene: UIScene) {
    }
}

Your interface creation code should thus be moved into the scene delegate's implementation of scene(_:willConnectTo:options:).

https://developer.apple.com/documentation/uikit/uiscenedelegate/3197914-scene

You will also want to move any Active or Foreground/Background code out of the app delegate and into the scene delegate. You should watch the WWDC 2019 videos on the multiple windows / multitasking under iOS 13, along with reading the docs:

https://developer.apple.com/documentation/uikit/app_and_scenes

Upvotes: 4

Related Questions