18 Value vs. Reference Types

This chapter builds on the lessons you have been learning about value types and references types. You will explore the differences between the two by comparing and contrasting 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 (e.g., a struct) or a reference type (e.g., a class).

Value Semantics

Create a new playground called ValueVsRef and save it in a good place. Your playground should have the same code below:

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

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.