Swift Code Review
Quick Reference
references/observable.md |
| throws, Result, try?, typed throws |
references/error-handling.md |
| Force unwraps, retain cycles, naming |
references/common-mistakes.md |
Review Checklist
- - [ ] No force unwraps (
!) on runtime data (network, user input, files) - [ ] Closures stored as properties use INLINECODE1
- [ ] Delegate properties are INLINECODE2
- [ ] Independent async operations use
async let or INLINECODE4 - [ ] Long-running Tasks check INLINECODE5
- [ ] Actors have mutable state to protect (no stateless actors)
- [ ] Sendable types are truly thread-safe (beware
@unchecked) - [ ] Errors handled explicitly (no empty catch blocks)
- [ ] Custom errors conform to
LocalizedError with descriptive messages - [ ] Nested @Observable objects are also marked @Observable
- [ ] @Bindable used for two-way bindings to Observable objects
When to Load References
- - Reviewing async/await, actors, or TaskGroups → concurrency.md
- Reviewing @Observable or SwiftUI state → observable.md
- Reviewing error handling or throws → error-handling.md
- General Swift review → common-mistakes.md
Review Questions
- 1. Are async operations that could run concurrently using
async let? - Could actor state change across suspension points (reentrancy bug)?
- Is
@unchecked Sendable backed by actual synchronization? - Are errors logged and presented with helpful context?
- Could any closure or delegate create a retain cycle?
Swift 代码审查
快速参考
references/observable.md |
| throws、Result、try?、typed throws |
references/error-handling.md |
| 强制解包、循环引用、命名规范 |
references/common-mistakes.md |
审查清单
- - [ ] 运行时数据(网络、用户输入、文件)不使用强制解包(!)
- [ ] 作为属性存储的闭包使用 [weak self]
- [ ] 委托属性使用 weak
- [ ] 独立的异步操作使用 async let 或 TaskGroup
- [ ] 长时间运行的 Task 检查 Task.isCancelled
- [ ] Actor 拥有需要保护的可变状态(无状态 actor 无意义)
- [ ] Sendable 类型真正线程安全(警惕 @unchecked)
- [ ] 错误被显式处理(无空 catch 块)
- [ ] 自定义错误遵循 LocalizedError 并提供描述性消息
- [ ] 嵌套的 @Observable 对象也标记为 @Observable
- [ ] @Bindable 用于与 Observable 对象的双向绑定
何时加载参考文档
- - 审查 async/await、actors 或 TaskGroups → concurrency.md
- 审查 @Observable 或 SwiftUI 状态 → observable.md
- 审查错误处理或 throws → error-handling.md
- 通用 Swift 审查 → common-mistakes.md
审查问题
- 1. 可以并发运行的异步操作是否使用了 async let?
- Actor 状态是否可能在挂起点发生变化(重入 bug)?
- @unchecked Sendable 是否有实际的同步机制支持?
- 错误是否被记录并以有用的上下文信息呈现?
- 是否有任何闭包或委托可能造成循环引用?