Value types

In the examples for references types, you saw the following snippet of code:

import UIKit
let point = CGPoint(x: 10, y: 10)
point.x = 10

At first sight, you might expect a value type to be a special kind of class because it looks like this snippet creates an instance of a class call CGPoint. You would be right in your observation, but your conclusion is wrong. CGPoint is not a class at all. Classes are inherently reference types, and they can't ever be something else. So what are value types, then?

There are two types of objects that are considered to be value types:

  • Structs
  • Enums

These two types are both very different, so let's make sure that you understand the basics of value types first, and then you'll learn what each of these two types is.

Let's have a look at the Pet example again, but use a struct instead of a class. Create a new Playground page in Xcode by opening the Project Navigator in your playground project and clicking the plus symbol in the bottom-left corner. This will allow you to add a new page to your Playground.

Add the following code to your new page:

struct Pet {
  var name: String
}

func printName(for pet: Pet) {
  print(pet.name)
  pet.name = "Jeff"
}

let dog = Pet(name: "Bingo")
printName(for: dog)
print(dog.name)

You will immediately notice that Xcode complains. The error you should see in the console tells you that pet is a let constant and you are not allowed to change its name. You can turn pet into a variable by updating printName as follows:

func printName(for pet: Pet) {
  var pet = pet
  print(pet.name)
  pet.name = "Jeff"
}

If you run your Playground now, make sure to look at the console closely. You'll notice that the pet's name remains unchanged in the second print. This demonstrates one of the key features of a value type. Instead of passing a reference to an address in memory around, a copy of the object is passed around. This explains why you aren't allowed to change properties on a value type that is assigned to a constant. Changing that property would change the value type's value, and it would, therefore, change the value of the constant. This also means that when you pass dog to printName, you pass a copy of the Pet instance to printName, meaning that any changes made to the instance are local to the printName function and won't be applied to dog in this case.

This behavior makes using value types extremely safe because it's tough for other objects or functions to make unwanted changes to a value type. And if you define something as a constant, it truly is a constant. Another characteristic of value types is that they're typically very fast and lightweight because they can exist on the Stack while reference types exist on the Heap. You'll learn more about this later when we compare reference types and value types.

Now that you have a basic understanding of value types, let's have a look at the specific value types: structs and enums.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset