Uplift iOS Interview
"Uplift iOS Interview" is a comprehensive guide to help aspiring iOS developers soar to new heights in their careers. This book is an indispensable tool for anyone looking to crack the iOS interview and impress their future employers with their technical prowess. With in-depth coverage of Swift, AutoLayout, SwiftUI, Multithreading, Memory management so on and so forth, this book is a treasure trove of knowledge for anyone looking to uplift their iOS development career.
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
Rev Up Your iOS Skills: Take a Dynamic Learning Journey

iOS Career Boost is the ultimate learning journey to elevate your iOS development career through a dynamic blend of visual learning, handy cheat sheets, coding practice materials, and expertly curated tips and tricks
Get Ready to Shine: Mastering the iOS Interview
- Uplift iOS Interview - A Comprehensive Guide to iOS Interview
- Xcode Cheat Sheet for Swift
- Xcode Cheat Sheet for SwiftUI
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. Your email address will only be used for the purpose of sending the newsletter and will not be shared with third parties or advertisers. Rest assured that we value your privacy and will not spam your inbox.
Connect with me on
Twitter and LinkedIn and don't hesitate to reach out with any questions about this post. Thank you for reading.