重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
modern-swift by johnrogers/claude-swift-engineering
npx skills add https://github.com/johnrogers/claude-swift-engineering --skill modern-swiftSwift 6.2 引入了严格的编译时并发检查,通过 async/await、actor 和 Sendable 约束,在编译时而非运行时防止数据竞争。这是安全并发 Swift 的基础。
现代 Swift 用编译器强制执行的安全性取代了旧的并发模式(完成处理程序、DispatchQueue、锁)。核心原则是:如果代码在启用严格并发检查的情况下能够编译通过,那么它就不会存在数据竞争。
| 需求 | 使用 | 避免 |
|---|---|---|
| 异步操作 | async/await | 完成处理程序 |
| 主线程工作 | @MainActor | DispatchQueue.main |
| 共享可变状态 | actor | 锁、串行队列 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 并行任务 |
TaskGroup |
DispatchGroup |
| 线程安全 | Sendable | 到处使用 @unchecked |
编写异步 Swift 代码时:
async 标记异步函数,用 await 调用@MainActoractor 替代锁来管理共享可变状态Task.isCancelled 或调用 Task.checkCancellation()如果内容有哪怕很小的可能需要,请务必加载参考文件。 拥有上下文总比遗漏模式或犯错要好。
| 参考资料 | 加载时机 |
|---|---|
| Concurrency Essentials | 编写异步代码、转换完成处理程序、使用 await |
| Swift 6 Concurrency | 使用 @concurrent、nonisolated(unsafe) 或 actor 模式 |
| Task Groups | 并行运行多个异步操作 |
| Task Cancellation | 实现长时间运行或可取消的操作 |
| Strict Concurrency | 启用 Swift 6 严格模式或修复 Sendable 错误 |
| Macros | 使用或理解 Swift 宏,如 @Observable |
| Modern Attributes | 迁移遗留代码或使用 @preconcurrency、@backDeployed |
| Migration Patterns | 现代化委托模式或 UIKit 视图 |
使用 @unchecked Sendable 作为快速修复 — 使用 @unchecked Sendable 来消除编译器错误意味着你放弃了安全性。如果在使用 @unchecked 后错误仍然存在,你的代码就存在潜在的数据竞争。应该修复根本问题。
调用点缺少 await — 调用异步函数时忘记 await 会导致编译器错误,但在循环中检查 Task.isCancelled 而不调用 Task.checkCancellation() 则会静默忽略取消操作。
在异步块中捕获 self 而未使用 weak — 在长时间运行的异步任务中持有对 self 的强引用会阻止其析构。在闭包中始终使用 [weak self],或者使用能自动管理生命周期的 .task。
未检查任务取消 — 长时间运行的操作应定期检查 Task.isCancelled 或调用 Task.checkCancellation(),否则会忽略取消信号。
忘记在 UI 代码和测试套件上使用 @MainActor — 更新 @Published 属性的主测试结构和视图模型需要 @MainActor。忘记使用它会静默允许跨线程修改。应对以下类型应用 @MainActor:视图模型、视图结构体、主测试结构体以及任何涉及 UI 的类型。
Actor 可重入性带来的意外 — actor 方法内部的 await 可能会暂时释放锁。另一个任务可能修改 actor 的状态。设计 actor 方法时,应假设状态在 await 点之间可能发生变化。
每周安装量
60
代码仓库
GitHub 星标数
180
首次出现
2026年1月23日
安全审计
已安装于
codex47
opencode45
claude-code42
gemini-cli41
github-copilot40
cursor36
Swift 6.2 introduces strict compile-time concurrency checking with async/await, actors, and Sendable constraints that prevent data races at compile time instead of runtime. This is the foundation of safe concurrent Swift.
Modern Swift replaces older concurrency patterns (completion handlers, DispatchQueue, locks) with compiler-enforced safety. The core principle: if it compiles with strict concurrency enabled, it cannot have data races.
| Need | Use | NOT |
|---|---|---|
| Async operation | async/await | Completion handlers |
| Main thread work | @MainActor | DispatchQueue.main |
| Shared mutable state | actor | Locks, serial queues |
| Parallel tasks | TaskGroup | DispatchGroup |
| Thread safety | Sendable | @unchecked everywhere |
When writing async Swift code:
async, call with await@MainActor to view models and UI-updating codeactor instead of locks for shared mutable stateTask.isCancelled or call Task.checkCancellation() in loopsALWAYS load reference files if there is even a small chance the content may be required. It's better to have the context than to miss a pattern or make a mistake.
| Reference | Load When |
|---|---|
| Concurrency Essentials | Writing async code, converting completion handlers, using await |
| Swift 6 Concurrency | Using @concurrent, nonisolated(unsafe), or actor patterns |
| Task Groups | Running multiple async operations in parallel |
| Task Cancellation |
@unchecked Sendable as a quick fix — Using @unchecked Sendable to silence compiler errors means you've opted out of safety. If the error persists after @unchecked, your code has a potential data race. Fix the underlying issue instead.
Missingawait at call sites — Forgetting await when calling async functions is a compiler error, but checking Task.isCancelled in a loop without calling Task.checkCancellation() silently ignores cancellation.
Capturingself in async blocks without weak — Holding a strong reference to in a long-running async task prevents deinit. Always use in closures or use which auto-manages the lifecycle.
Weekly Installs
60
Repository
GitHub Stars
180
First Seen
Jan 23, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex47
opencode45
claude-code42
gemini-cli41
github-copilot40
cursor36
| Implementing long-running or cancellable operations |
| Strict Concurrency | Enabling Swift 6 strict mode or fixing Sendable errors |
| Macros | Using or understanding Swift macros like @Observable |
| Modern Attributes | Migrating legacy code or using @preconcurrency, @backDeployed |
| Migration Patterns | Modernizing delegate patterns or UIKit views |
self[weak self].taskNot checking task cancellation — Long-running operations should regularly check Task.isCancelled or call Task.checkCancellation(), otherwise cancellation signals are ignored.
Forgetting@MainActor on UI code and test suites — Main test struct and view models that update @Published properties need @MainActor. Forgetting it silently allows cross-thread mutations. Apply @MainActor to: view models, view structs, main test structs, and any type that touches UI.
Actor re-entrancy surprises — await inside an actor method can release the lock temporarily. Another task may modify actor state. Design actor methods assuming state can change between await points.