0Pricing
Swift Academy · Lesson

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

  1. How ARC Counts References
  2. Retain Cycles: Causes and Detection
  3. weak and unowned References
  4. Closure Capture Lists and [weak self]
← Back to Swift Academy