ARC basics: strong / weak / unowned
Learn ARC reference counting and when to use strong , weak , and unowned to avoid memory leaks.
ARC in one minute
Swift uses ARC (Automatic Reference Counting). Each strong reference increases a class instance's count. When it drops to zero, deinit runs and memory is freed.
Strong by default
Strong is the default. When the last strong reference goes away, deinit is called.
final class Person {
let name: String
init(_ name: String) { self.name = name; print("init:", name) }
deinit { print("deinit:", name) }
}
var p: Person? = Person("Ada")
p = nil // ARC count -> 0, triggers deinitAll lessons in this course
- ARC basics: strong / weak / unowned
- Retain cycles & breaking with weak/unowned
- Value types to reduce sharing