iOS Tutorial [SWIFT] – What Are Value and Reference Types?

In this iOS tutorial SWIFT Value and Reference Types, we’re going to show you what value and reference types are and how to use them through some great examples.

You might be interested in SWIFT – Access iOS Camera and Photo Library

Value types

Value Types: Each instance keeps a unique copy of its data, and is usually defined as a struct, enum, or tuple.

Now let’s create a struct to show you how value types operate:

struct myStruct

{

var value:Int = 10

}

var a = myStruct()

var b = a

a.value = 16

print("\(a.value), \(b.value)")

While only being 8 lines of code, quite a few things are going on over here. Firstly, we create a struct called “myStruct”. As defined earlier, structs are a value type, which means that their copies will create an independent instance. Inside our struct, we create an integer variable (also a value type) and set it equal to 10. In the following line, we create an instance of myStruct and call it “a”. After that, we create a variable called “b” and copy a’s value into b. At the current moment, the value of b.value is 10. In the next line, we change the value of “a” to 16. This will not change the value of “b”, and this is because these objects are pointing towards their own memory storage.

Reference Type

Reference Types: Instances share a single copy of the data, and the type is usually defined as a class.

Here is an example of a Reference Type using a class:

class Animal

{

var animalType:String = "Dog"

}

var animal1 = Animal()

var animal2 = animal1

animal1.animalType = "Cat"

print("\(animal1.animalType), \(animal2.animalType)")

This code looks very similar to our struct above. Here we create a class called “Animal” and give it a string property called “animalType” and set it equal to “Dog”. In the next line, we create an instance of “Animal” called “animal1”. In the next line, we create another variable called “animal2” and copy the value from animal1 to animal2. The next line in which we alter the value for animal1.animalType DOES effect animal2.valueType. Why? When copying a reference type, you create a shared instance. What that means is that both animal1 and animal2 are pointing towards the same memory storage, hence the word “shared”. Therefore, their properties will be the same according to whichever one got updated the latest.

Recommended for you Swift 4 – Top 3 new String API features

Classes and Structures

When you copy a reference type (i.e. a class), you create a shared instance. This means that after you copy your variable to another, they will both retrieve their value from a single instance of the data. So when you alter the value of the second variable, it will affect the first one. I know this sounds repeated, but more often than not, different ways of defining this process can prove to be beneficial to most.

Now let’s dive into what classes and structures have in common: (This is from the iOS Developer Library)

  • Define properties to store values
  • Define methods to provide functionality
  • Define subscripts to provide access to their values using subscript syntax
  • Define initializers to set up their initial state
  • Ability to expand their functionality beyond default implementation
  • Conform to protocols to provide standard functionality

It should also be noted that assigning a variable/constant to a class instance, only increases the reference count to the underlying value. This applies to functions, which also are reference types.

Variables and Constants in Regards to Value/Reference Types:

Changing the value of properties in classes and structs isn’t an issue when it comes to variables, but constants work differently. When using a constant, your reference must remain constant. Similarly, when working with value types, your value must remain constant.

In simpler terms: When using a class, you can mutate the properties of variables and constants with a hassle. However, when working with a struct, you may not change the value of a property of a constant. You will have to change them to variables.

If you liked this iOS tutorial SWIFT Value and Reference types, take a look at some of our other stuff at the tutorial section.

You might be interested in I ❤ Swift [Part 2] – Multiple UITableViewCells in UITableView

Leave a Reply

Your email address will not be published. Required fields are marked *