Member-only story

Swift Memory Mastery: Navigating ARC in Swift & SwiftUI

Mohammad Mahmudul Hasan
4 min readFeb 14, 2025

Understanding Strong, Weak, and Unowned References in Swift and SwiftUI

In Swift, memory management is handled automatically by Automatic Reference Counting (ARC). However, when you have two objects that refer to each other, you must be careful to avoid retain cycles (where neither object is released because they keep each other alive). Swift provides three types of references to manage these relationships:

Strong References (default):

  • What they do: When you assign an object to a property or variable without specifying otherwise, Swift creates a strong reference. This means that the reference count of the object is incremented, and the object will not be deallocated as long as this strong reference exists.
  • Example:
class Person {
let name: String
init(name: String) {
self.name = name
}
}

var john: Person? = Person(name: "John")
// 'john' holds a strong reference to the Person instance.
john = nil // Now the Person instance can be deallocated.

Weak References:

  • What they do: A weak reference does not increase the reference count. It’s used when the referenced object might become nil later (i.e., it might be deallocated), so it is declared as an…

--

--

Mohammad Mahmudul Hasan
Mohammad Mahmudul Hasan

Written by Mohammad Mahmudul Hasan

A learner, constantly striving to learn new technologies and look the ways to be better in this rapidly changing industry.

No responses yet