weak and unowned References
Choosing between weak (Optional) and unowned (non-Optional) to break cycles.
Welcome
Breaking retain cycles requires references that don't increment the ARC count. Swift provides two: `weak` (optional, zeroed automatically) and `unowned` (non-optional, unsafe if nil).
weak References
```swift
class Owner { var pet: Pet? }
class Pet { weak var owner: Owner? } // does not retain
var o: Owner? = Owner()
var p: Pet? = Pet()
o?.pet = p
p?.owner = o
o = nil // Owner deinit — p.owner becomes nil automatically
```
All lessons in this course
- How ARC Counts References
- Retain Cycles: Causes and Detection
- weak and unowned References
- Closure Capture Lists and [weak self]