O'Reilly logo

Mastering iOS 10 Programming by Donny Wals

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

Understanding value types

You just saw how passing around a reference type can yield results you might not expect. The idea behind a value type is that this doesn't happen because instead of passing around references to addresses in memory, you're passing around actual values. Doing this will often lead to safer and more predictable code. As if this isn't enough benefit on its own, value types are also cheaper to instantiate than reference types. More on that later. We'll focus on the most visible differences first.

Differences between values and references in usage

Let's take a look at the Pet example again; this time we'll use a struct instead of a class:

struct Pet { var name: String init(name: String) { self.name = name } } func printName(forPet ...

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