Why not assign MainWindowController to the let right away?

I know this question has a lot of Swift-related issues in it, and that will be covered later, but…

In the AppDelegate book example in Chapter 1 we seem to do a much of var/let dancing. I wonder considering the MainWindowController is going to be instantiated right away anyways if this might be a more lean implementation.

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    let mainWindowController = MainWindowController(windowNibName: "MainWindowController")

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        mainWindowController.showWindow(self)
    }
    
}

I suppose there is a risk the MainWindowController init might fail (maybe a typo in that string) but I’d consider that a compile time behavior not a run time behavior.

Good question. You’re right, it could be streamlined as shown, but we wanted to demonstrate a more flexible window controller pattern that allows for the window controller to not exist all the time. If this were a preferences window, for example, you probably wouldn’t want to have the PreferencesWindowController in memory except when it is visible. Thus the optional (var) property.

Does that help?

Awesome. Figured as much. Thanks for the info.