Swift Memory Management
Priority: P0
Implementation Guidelines
ARC Fundamentals
- •Default: Strong references. Swift automatically manages retain/release.
- •Weak: Use
weakfor delegate patterns and parent-child relationships. - •Unowned: Use
unownedonly when reference guaranteed to outlive (rare).
Capture Lists
- •Closures: Always use
[weak self]or[unowned self]in escaping closures. - •Self in Structs: No capture list needed (
selfis copied by value). - •Multiple Captures:
[weak self, weak delegate].
Retain Cycles
- •Delegates: Always
weak var delegate. - •Closures as Properties: Use
weakorunownedin capture list. - •two-way References: One side must be
weak.
Anti-Patterns
- •Strong Delegates:
**No strong var delegate**: Use weak. - •Missing Capture List:
**No self in escaping closures**: Use [weak self]. - •Unowned Misuse:
**Avoid unowned**: Use weak unless certain.