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; becomesnilwhen the referenced object deallocates- Swift 6.2 allows
weak let(SE-0481) for a weak reference you never reassign; before thatweakhad to bevar. Afinal classwith aweak letof a Sendable type can beSendable unowned: non-optional, nonilcheck; traps if the object is already gone- Use
weakfor delegates, parent back-references that can outlive the parent, and escaping closures with uncertain timing - Use
unownedwhen 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
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
weakwhen unsure about lifetime; it's safe by default - Use
unownedonly for guaranteed lifetime relationships, and document why - Use
weak let(Swift 6.2+) when the reference is set once ininit - Constrain delegate protocols to
AnyObjectso they can be held weakly - Avoid
[unowned self]in closures that might run afterselfis gone
Related concepts
- Retain cycles, ARC, capture lists, closures,
Sendable