Var and let mainWindowController question

Hi all,

I am a beginner and (on page 38) I can’t understand the utility to create both a variable and a constant mainWindowController. I’m sorry maybe I will understand more by reading following chapters of the book, but to date I’m not able to understand what’s the relation between the two mainWindowController-s.

[code]var mainWindowController: MainWindowController?

let mainWindowController = MainWindowController(windowNibName: “MainWindowController”)[/code]

Somebody can help? :blush:

It will be helpful to see the context in which the two declarations for mainWindowController were made so I’m going to post all the code for the AppDelegate.swift file.

[code]class AppDelegate: NSObject, NSApplicationDelegate {

var mainWindowController: MainWindowController?

func applicationDidFinishLaunching(aNotification: NSNotification) {
	// Create a window controller with a XIB file of the same name
	let mainWindowController = MainWindowController()
	// Put the window of the window controller on screen
	// Set the property to point to the window controller
	self.mainWindowController = mainWindowController

func applicationWillTerminate(aNotification: NSNotification) {
	// Insert code here to tear down your application


The first declaration, var mainWindowController: MainWindowController?, declares an optional variable meaning it may or may not have a value associated with it, of a MainWindowController class object. At this point it does not have any value stored in it as nothing has been assigned to it. (Strictly speaking, that is not entirely true but it will do for the purposes of this discussion.)

The second declaration, let mainWindowController = MainWindowController(), inside the function applicationDidFinishLaunching(), declares a new constant with the same name and assigns to it an instance of a MainWindowController object. Having a constant (or a variable for that matter) with the same as the class variable is allowed because the two declarations are made in different scopes. Scopes will explained in Chapter 4.

The program now has an actual MainWindowController object with which it can work. The following line of code, mainWindowController.showWindow(self), causes the window to be displayed on the screen and the last line of code, self.mainWindowController = mainWindowController, assigns the value in the constant (a pointer to the MainWindowController object that was created) to the class variable so it will be retained by the program when the function has finished and returns to its caller.

This is a common pattern in Cocoa programming. Inside a function you will create a constant that you will do something with before assigning it to an optional variable to be retained when the function has finished.

Thank you so much Brian, I was a little confused by the use of all of these similar terms, excellent explanation.

I’m glad I was able to help. One more point I should have mentioned before is that the first declaration for the mainWindowController variable makes that variable a member of the AppDelegate class. The constant declaration inside the applicationDidFinishLaunching() function is a local variable available only within that function.

Good luck with your continued studies.