O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 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 hecate ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required