[ARC] Unowned Reference Count and Deallocation Timing

Semantically, it’s best to think of these as three types of references: strong, weak, and unowned. Each strong reference type will increment the reference count. As each strong reference is released, the reference count is decremented. When the reference count hits 0 (i.e., there are no more strong references), the object will be deallocated.

Once deallocated, weak references will be set to nil, but unowned references will not. [Technically, the unowned references in Swift are not just dangling pointers. They’re safer than that, resulting in more predictable runtime errors if you misuse them.]

As you’ve seen, the internal implementation is richer than this, but this is probably the best mental model from the perspective of a normal Swift programmer. For more information, see Automatic Reference Counting in The Swift Programming Language.

2 Likes