Self.mainWindowController

‘need to set the “property” to mainWindowController’

What is this a property of? I don’t grasp what seems like a circular statement where the property is set to itself?

Where in the chapter is the text you refer to?

[quote=“drmark”]‘need to set the “property” to mainWindowController’

What is this a property of? I don’t grasp what seems like a circular statement where the property is set to itself?[/quote]

I wasn’t able to find the text you seem to be quoting above, but perhaps you mean the line of code that reads:

This is described in the Let’s Get Started chapter in the section entitled Showing the Window, but in short what this line does is assign the mainWindowController property on self to reference what the mainWindowController local variable references (the instance of MainWindowController). Both have the same name, so we use “self.” to tell the compiler that we want the property.

mainWindowController is the name of a property in the AppDelegate class that Xcode automatically created. When you define a class, you can specify properties, for instance:

class Dog { var name: String ... ... }

name is called a property of the Dog class.

Here’s the deal: programmers are lazy and trying to think up of different variable names for the same thing can be too brainwave intensive. The code could have been written like this:

class AppDelegate {
    
    var x: MainWindowController  #<----  A property of the AppDelegate class

    //   +—-  A parameter variable of the init() method
    //   |
    //   V
    init(y: MainWindowController) {
        x = y    //Assign the parameter variable y to the property x
    }

}

class MainWindowController {
    
}

let z : MainWindowController = MainWindowController();

let myAppDelegate = AppDelegate(y: z)   //Create a new instance of the AppDelegate class, and assign z 
                                      //to the parameter variable y in AppDelegate's init() method 

But it’s hard to figure out what is going on there with the variables x, y, and z; so programmers try to use descriptive variable names: a variable name should describe what has been assigned to it. Let’s rewrite the example with descriptive variable names:

[code]class AppDelegate {

var myMainWindowController: MainWindowController


init(aMainWindowController: MainWindowController) {
    myMainWindowController = aMainWindowController
}

}

class MainWindowController {

}

let thisMainWindowController = MainWindowController();

let myAppDelegate = AppDelegate(aMainWindowController: thisMainWindowController)[/code]

Okay, that works, too; but it’s just too brainwave intensive for programmers to have to write my/this/a in front of the variable name mainWindowController just to create different names. And it’s hard to keep track of when you are writing the code whether you need to write the variable name with my/this/a in front of it.

Because the variables are used in different places in the code–where they don’t cause any interference with each other–let’s just use the same variable name, namely myWindowController for all the variables:

[code]class AppDelegate {

var mainWindowController: MainWindowController


init(mainWindowController: MainWindowController) {
    mainWindowController = mainWindowController  
}

}

class MainWindowController {

}

let mainWindowController = MainWindowController();

let myAppDelegate = AppDelegate(mainWindowController: mainWindowController)[/code]

Unfortunately, the same named variables do interfere with each other here:

    init(mainWindowController: MainWindowController) {
        mainWindowController = mainWindowController    //****THIS LINE IS FLAGGED WITH AN ERROR***

    }

The error essentially says,

(Technically, the error is saying that the parameter variable mainWindowController is a ‘let’ variable, i.e. a constant, so that variable name cannot appear on the left hand side of an assignment statement.)

However, if you write:

Swift can easily sort out that the thing on the left hand side of the assignment statement is the variable here:

class AppDelegate {
    
    var mainWindowController: MainWindowController  //<--- self.mainWindowController
    
    
    init(mainWindowController: MainWindowController) {
        self.mainWindowController = mainWindowController  
    }
    
}

…and the thing on the right hand side of the assignment statement is the variable here:

class AppDelegate {
    
    var mainWindowController: MainWindowController 


    //         +————  This variable
    //         |
    //         V
    init(mainWindowController: MainWindowController) {
        self.mainWindowController = mainWindowController  
    }
    
}

So a typical idiom in Java, C++, etc., and apparently also in Swift, is to write initializer methods with parameter names that are the same as the names of the properties you want to assign them to, for instance:

class Dog {
    var name: String
    var age: Int
    
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}

But you don’t have to do that. It’s just a convenience. When you write your own initializers, try writing them like this for awhile:

init(aName: String, anAge: Int) { name = aName age = anAge }

And if it doesn’t bug you to have to think up different names for the parameter variables, then stick with it.

^^ Thanks for the explanation

This may be for the errata section but I have the 5th edition in Kindle and in the RGBWell section the AppDelegate.swift file instructions contains

// Set the property to point to the window controller

self.mainWindowController = mainWindowController

this is tagged as an error. I notice in the discussion that ir is enclosed in an Init.