Constant Value and Reference Types

Value and reference types behave differently when they are constants. Create a new struct called Pantheon so that you have a value type of your own to work with.

Listing 18.7  Making the Greek Pantheon

...

class GreekGod {
    var name: String
    init(name: String) {
        self.name = name
    }
}
let hecate = GreekGod(name: "Hecate")
let anotherHecate = hecate

anotherHecate.name = "AnotherHecate"
anotherHecate.name
hecate.name

struct Pantheon {
    var chiefGod: GreekGod
}

The new struct above represents the Greek pantheon. It has one stored property to reflect the foremost god in the pantheon. Greek gods are always squabbling, so you made this property mutable with var.

Make a new instance of Pantheon with ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.