Value and reference types in Swift -A deep dive

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

The value and reference types are a very important concept in Swift. A value type contains the data within its own memory. A reference type holds a pointer to another memory location that holds the actual data.

In this article, I will explain:

  • The basics of value and reference types
  • Differences between value and reference types
  • Why does Apple prefer to use value types by default?
  • When to use value types or reference types

The basics of value and reference types

A Value type holds its contents in memory allocated on the stack. When you create a Value type, one single space in memory is allocated to store the value. Here, the variable directly holds a value. Let’s assume, you assign it to another variable. The value is copied directly and both variables work independently. But, their addresses are different.


A reference type holds a memory address to the object but not the actual object itself. It represents the address of the variable rather than the data itself. Assigning a reference variable to another variable doesn’t copy the data. Instead, it creates a second copy of the reference, which refers to the same address as the original value. Unlike value types, a reference type doesn’t store its value. Instead, it stores the address where the value is being stored. A reference type contains a pointer to another memory location that holds the data.

Differences

The main difference is the value types copy data while reference types share a single copy of their data. Value Type is immutable. When we create an instance with a value type its create a unique copy of data. The reference type is mutable which will not create a unique copy of data.


Class is the Swift representation of a reference. Every inherited item from NSObject is a reference type in Objective-C.
Struct, enum, and tuples are the most used value types in Swift. NSInteger is a value type in Objective-C.

Common value types are

  • struct
  • enum
  • dictionary
  • set
  • tuple

Common reference types are

  • Class
  • Closure
  • Function

Why does Apple prefer to use value types by default?

Swift value types are powerful. They can adopt protocols to get the behaviour through default implementations. Value types automatically thread-safe due to not being shareable. Its instances are safe in a multi-threaded environment. Many threads can mutate the instance. We can use value types without having to worry about race conditions or deadlocks. 


Value types(Struct) are preferable. Copying a value is way safer than having many references to the same instance. You can always send a copy of your variable to other places. You never have to worry about that other place changing the value of your variable in the value type case.

Value type has no references unlike reference type; so there are no memory leaks. (Keep in mind, If you capture a struct inside a closure, it basically captures a reference to the instance).


Using value types such as struct or enum makes it comparatively easier. You can concentrate on the section of code without considering the overall state of the app. Local changes on value types aren’t visible to the rest of the app, unlike classes. This philosophy gives the confidence to play with the instance of the code section.

When to use value types or reference types

Consider the following recommendations to choose a new data type for your app.

  • Try structures by default.
  • Make use of classes when you need Objective-C interoperability.
  • Choose classes when you need to control the identity of the data you’re modelling.
  • Use structures along with protocols to adopt behaviour by sharing implementations.

Read from apple documentation



✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn



Get Ready to Shine: Mastering the iOS Interview




Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.