← Home

weak vs unowned in Swift

In Swift, weak is usually used to break reference cycles; yet there are performance differences between it and unowned.

weak adds the cost of an optional check and ARC bookkeeping on every access, and becomes nil when the reference is freed. unowned avoids that overhead, but should only be used when you’re sure of the reference’s lifetime — otherwise you risk a crash. In short: if the lifetime is guaranteed, unowned; if not, weak.