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

Using Value and Reference Types Together

You may be wondering, Can I put a value type inside of a reference type? Can I put a reference type inside of a value type? The answer to both of these questions is Yes, and you did the latter by adding a property of the class GreekGod to Pantheon. However, although we led you to do this without warning, you must be very careful about using a reference type inside of a value type. (Using a value type inside a reference type does not present any particular problems.) Consider the following example that changes the name of hecate.

Listing 18.12 The Romans are coming

...
let pantheon = Pantheon(chiefGod: hecate)
let zeus = GreekGod(name: "Zeus")
zeus.name = "Zeus Jr."
zeus.name

pantheon.chiefGod.name ...

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