dr.calix
dr.calix

Reputation: 727

type of 'window' has different optionality than required by protocol 'uiapplicationdelegate' after XCode update to 6.3

I have this code var window = UIWindow() in my AppDelegate. My app is working fine before. After I updated my XCode to 6.3, I can no longer run my iOS app in simulator as I am getting the error

type of 'window' has different optionality than required by protocol 'uiapplicationdelegate'

Upvotes: 4

Views: 3896

Answers (3)

Sibelius Seraphini
Sibelius Seraphini

Reputation: 5633

In Swift 2, AppDelegate have:

var window: UIWindow?

instead of

var window: UIWindow

because it should be nil

You can use a lazy var to make code simply

lazy var window: UIWindow? = {
    let win = UIWindow(frame: UIScreen.mainScreen().bounds)
    win.backgroundColor = UIColor.whiteColor()
    win.rootViewController = UINavigationController(rootViewController: self.authViewController)
    return win
}()

Upvotes: 2

dr.calix
dr.calix

Reputation: 727

Thanks for all your contributions. I am not really sure about the reason why suddenly my code window declaration is no longer working. To fix it, I used the answer from here: https://stackoverflow.com/a/25482567/2445717

I revert the declarion of window to the default: var window: UIWindow?

and then used the code below for didFinishLaunchingWithOptions

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    if let window = window {
        window.backgroundColor = UIColor.whiteColor()
        window.rootViewController = ViewController()
        window.makeKeyAndVisible()
    }

Upvotes: 2

emrys57
emrys57

Reputation: 6806

If you cmd-click on the word UIApplicationDelegate in the class definition of your code, you will open the protocol definition. I suspect you are using this call:

  func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) {...}

and that may have changed in Swift 1.2, but does not seem to be widely documented. If you wrote instead

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) {...}

then you would get the error message you report.

This particular problem is not fixed up by the automated program Daniel Nagy mentions - I ran into a similar issue.

If you have provided that optional function, then just add a ? after the UIWindow in the function definition.

Upvotes: 0

Related Questions