Reference Semantics

Reference semantics work differently than value semantics. With value types, you get a copy of the instance when you assign it to a new constant or variable. The same is true when you pass an instance of a value type as the argument to a function. But for an instance of a reference type, these two actions create an additional reference to the same underlying instance.

To see what this means, you are going to model a company and its employees, who for some reason have a hard time keeping their employee IDs straight. Add a new class to the playground to model a company’s employee:

Example 18.3. Adding an Employee class
...
playgroundGreeting += "! How are you today?"               "Hello, playground! How...
str                                                        "Hello, playground"

class ...

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition 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.