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

18 Value vs Reference Types

This chapter builds on the lessons you have been learning about value types, like structs, and references types, like classes. You will explore the differences between the two by comparing their differing behaviors in a variety of scenarios. At the end of this chapter, you should have a good understanding of when to use a value type or a reference type.

Value Semantics

Create a new playground called ValueVsRefs. Your playground has the usual template code:

import Cocoa

var str = "Hello, playground"

You have seen this code many times before: You have a mutable instance of type String set to the value of "Hello, playground". Make a new string by giving the value of str to another instance. ...

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