Reference and Value types in Swift — What are they?

Mammadowr
4 min readOct 4, 2022

--

Visual explanation of reference and value types

This is one of the most important topic for every iOS Developers. If you are beginner, take your coffee and sit closer. 🙂

Before we start, if you did not check yet, check my other articles:

If you are ready, let’s start. 🥳

There are two types in Swift. The first is the value type, includes struct, enum, Tuple, Int, Bool, String and Dictionary. Each of the data you define with one of these types is synchronised with a unique value in memory.

The other category, class, can be given as an example of reference type. Even if we assign an object created from a class to dozens of different variables, the corresponding value in memory will remain the same. As it is clear from its name, it will reference the first value instead of assigning a new value for each variable.

We create a class named Planes and create an instance named firstPlane. At the same time, we define the brand property of the variable firstPlane as “Boeing”. Then we create the variable named secondPlane from the firstPlane type. We define the brand property of the secondPlane variable as “Airbus”. When we look at the output, we see that firstPlane and secondPlane give the same output:

Reference type

This may surprise you a bit at first glance, but since reference types point to a single point in the memory as I just mentioned, all the changes we make change a single point in the memory. Let’s try the same example with struct, a value type:

Value type

As you can see, things have changed a bit more this time, the value types create a new copy in memory every time we define a variable. There is no connection between firstPlane and secondPlane and they are kept in memory independently of each other.

I hope things are becoming clearer, now I would like to share with you one of my favourite graphs on this subject:

Which one to Prefer?

Situations where you should use value type:

  • Data is compared with ==
  • When it is necessary to produce copies in independent state
  • When the data will be used with multi-thread

Situations where you should use reference type:

  • Data is compared with ===
  • When a shared variable is used

Of course, both types have positive and negative sides, the important thing is to use the useful one.

Advantages of Value Type

Efficiency: Value types are stored on the stack in RAM. Accessing the stack is preferred because it is much faster than accessing the heap.

Thread Safe: Value types create independent copies each time. Performing more than one operation on the same thread may cause the programme to crash, at this point, choosing a value type will be a safer way.

Memory Leak: ARC cleans the data in Swift, for example, when we create a class, it keeps the information about how many times the class is used and deletes that value from the memory when the number drops to 0. But since the value type does not have a reference, memory leak does not occur.

Predictability: Since value types create a new copy each time, they are not interested in where it was used before. In reference types, on the other hand, we need to know the last referenced value, value types provide more comfortable use in this regard.

Advantages of Reference Type

Inheritance: The inheritance feature of OOP can be used in reference types.

Common Operation: Reference types keep a single place in memory and update it if necessary. If you have a variable that will be updated continuously over the same data, it will be easier to use a single reference.

Basically that is all from my side. If you like my articles, if you think that they are useful, you can click on “Follow” button and share articles to reach more people. :)

--

--

Mammadowr

I speak 6 human and 5 programming languages. 🥳  iOS Developer. To support me: https://www.buymeacoffee.com/mammadowr8 ☕️