weak vs unowned

The interviewer asks

“What's the difference between weak and unowned references? When do you use each?”

How to answer

Both are non-owning references that don't increase the retain count, so both break strong reference cycles under ARC. A weak reference is always optional and is automatically set to nil when the object is deallocated. An unowned reference is non-optional and assumes the object outlives it; accessing it after deallocation is a guaranteed crash. I default to weak and use unowned only when the lifetime relationship is structural, like a child that can't exist without its parent.

Key points

  • weak: always optional; becomes nil when the referenced object deallocates
  • Swift 6.2 allows weak let (SE-0481) for a weak reference you never reassign; before that weak had to be var. A final class with a weak let of a Sendable type can be Sendable
  • unowned: non-optional, no nil check; traps if the object is already gone
  • Use weak for delegates, parent back-references that can outlive the parent, and escaping closures with uncertain timing
  • Use unowned when the referenced object always outlives the reference (e.g. a credit card that always belongs to a customer)
  • unowned(unsafe) skips the runtime check entirely; avoid it outside performance-critical code

If they dig deeper

How ARC tracks weak references (side tables) and why weak has a small cost. Capture lists in closures ([weak self] vs [unowned self]) and the guard let self pattern. Why Task { } holding self strongly isn't a cycle unless self stores the task. Why delegates are weak var delegate: Delegate? and the protocol must be AnyObject-constrained.

Common pitfalls

Using unowned where the object can be deallocated first (a crash, not a nil). Making a delegate property strong and creating a cycle. Adding [weak self] to non-escaping closures where no cycle is possible. Using [unowned self] in a network callback that may complete after the screen is dismissed. Assuming weak must always be var in a Swift 6.2+ codebase.

How to use it

swift
protocol ListDelegate: AnyObject { func didSelect(_ index: Int) }

final class ListController {
    weak var delegate: ListDelegate?            // weak: delegate may go away first
}

final class Customer {
    var card: CreditCard?                       // owns the card
}

final class CreditCard {
    unowned let customer: Customer              // a card never outlives its customer
    init(customer: Customer) { self.customer = customer }
}

final class Session: Sendable {
    weak let owner: Owner?                      // Swift 6.2: weak let (SE-0481)
    init(owner: Owner) { self.owner = owner }
}

@MainActor
final class ProfileController {
    var name = ""
    var onRefresh: (() -> Void)?

    func bind() {
        onRefresh = { [weak self] in            // closure stored by self: use weak
            guard let self else { return }
            self.name = "Updated"
        }
    }
}

Best practices

  • Prefer weak when unsure about lifetime; it's safe by default
  • Use unowned only for guaranteed lifetime relationships, and document why
  • Use weak let (Swift 6.2+) when the reference is set once in init
  • Constrain delegate protocols to AnyObject so they can be held weakly
  • Avoid [unowned self] in closures that might run after self is gone

Related concepts

  • Retain cycles, ARC, capture lists, closures, Sendable