Agent-skills-standard swift-memory-management
Prevent retain cycles via ARC, weak/unowned references, and Capture Lists in Swift. Use when managing Swift ARC, avoiding retain cycles, or configuring capture lists in closures. (triggers: **/*.swift, weak, unowned, capture, deinit, retain)
install
source · Clone the upstream repo
git clone https://github.com/HoangNguyen0403/agent-skills-standard
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HoangNguyen0403/agent-skills-standard "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/swift/swift-memory-management" ~/.claude/skills/hoangnguyen0403-agent-skills-standard-swift-memory-management && rm -rf "$T"
manifest:
skills/swift/swift-memory-management/SKILL.mdsource content
Swift Memory Management
Priority: P0
Implementation Guidelines
ARC Fundamentals
- Default: Strong references. ARC handles retain/release automatically.
- Weak: Use weak if the reference can become nil during its lifetime (delegates, optional parent refs).
- Unowned: Use unowned if the reference is guaranteed to outlive referring object (rare; prefer
).weak
Capture Lists
- Closures:
at beginning of the closure's capture list. Pattern:[weak self]
.{ [weak self] in guard let self = self else { return } } - Self in Structs: No list needed —
copied by value.self - Multiple Captures:
.[weak self, weak delegate]
Retain Cycles
- Delegates:
always. Protocol inherits from AnyObject (e.g.,weak var delegate
)..protocol MyDelegate: AnyObject {} - Closures as Properties: Use
orweak
in capture list.unowned - Two-way References: One side must
.weak
Anti-Patterns
- No strong var delegate: Use weak.
- No self in escaping closures: Use [weak self].
- No unowned unless certain: Default to weak to prevent crashes.