2 var with same name (isDiscoTime)?

In chapter 11, for the CatNap game, I don’t understand how there can be 2 variables with the same name:

private var isDiscoTime: Bool = false {
    didset {
        ...
    }
}

static private(set) var isDiscoTime = false

When I use the name isDiscoTime in my code, how can Swift know which one is the right one?

The key is the word static. Without static, isDiscoTime is an instance variable and each instance of the type will have its own individual value. With static, isDiscoTime belongs to the class instead of the instance.

So, if you want to refer to the property for an individual disco ball node, you might type discoBallNode1.isDiscoTime, but for the static property you’d type DiscoBallNode.isDiscoNode. Because the latter uses the class’s name, Swift knows you’re referring to the static property.

2 Likes

Thanks for the explanation!