swift
Write safe Swift code avoiding memory leaks, optional traps, and concurrency bugs.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
Quick Reference
| Topic | File |
|---|---|
| Optionals, nil safety, force unwrap | optionals.md |
| Retain cycles, weak refs, closures | memory.md |
| async/await, actors, Sendable, value types | concurrency.md |
| JSON encoding/decoding traps | codable.md |
| Protocols, collections, strings, errors, build | types.md |
| SwiftUI state (@State, @Binding, Combine) | swiftui.md |
| Property wrappers, actors, result builders, macros | advanced.md |
| XCTest pitfalls, SPM gotchas | testing.md |
Critical Rules
Memory & Safety
- Force unwrap
!crashes on nil — useguard letorif letinstead - Closures capturing
selfstrongly create retain cycles — use[weak self]in escaping closures - Delegates must be
weak— strong delegate = object never deallocates -
try!crashes on any error — never use in production paths -
removeFirst()crashes on empty — usepopFirst()for safety
Concurrency
-
async letstarts immediately — not when youawait - Actor reentrancy at every
await— state may change between suspension points -
@MainActordoesn't guarantee immediate main thread — it's queued -
Sendableconformance violations crash at runtime — compiler warnings are errors
Types & Collections
- Protocol extensions don't override — static dispatch ignores subclass implementation
- Mutating struct in collection requires reassignment —
array[0].mutate()doesn't work -
String.Indexfrom one string invalid on another — even if contents match
SwiftUI
-
@StateObjectowns,@ObservedObjectborrows — recreating view loses ObservedObject state -
@EnvironmentObjectcrashes if not injected — no compile-time check - View identity change resets all
@State— changing ID loses state
Build
-
print()builds strings even in release — remove or use os_log - Generic code bloat — specialized for each type, increases binary size
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-03-11
- 索引更新时间
- 2026-03-11
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!