5 concepts · 10 practice questions
Automatic Reference Counting tracks how many strong references point at each class instance. The compiler inserts the retain and release calls, and the instance is freed, running deinit, as soon as the count reaches zero. There is no background collector, so memory is freed predictably, but ARC cannot detect cycles: two objects holding each other strongly are never freed. Structs and enums are not reference counted themselves, though any class references they store are.
final class Session {
let id: String
init(id: String) { self.id = id }
deinit { print("Session \(id) freed") }
}
var first: Session? = Session(id: "a") // count 1
var second = first // count 2
first = nil // count 1
second = nil // count 0: prints "Session a freed"
Sign in to read the rest and take the 10-question test.
Sign in to continue