Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

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'

like image 543
dr.calix Avatar asked Oct 20 '22 14:10

dr.calix


2 Answers

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()
    }
like image 98
dr.calix Avatar answered Oct 22 '22 23:10

dr.calix


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
}()
like image 38
Sibelius Seraphini Avatar answered Oct 22 '22 23:10

Sibelius Seraphini