swift-expert by jeffallan/claude-skills
npx skills add https://github.com/jeffallan/claude-skills --skill swift-expert验证检查点: 第 3 步之后,运行
swift build以验证编译。第 4 步之后,运行swift build -warnings-as-errors以显示 Actor 隔离和 Sendable 警告。第 5 步之后,运行swift test并确认所有异步测试通过。
根据上下文加载详细指导:
| 主题 | 参考 | 加载时机 |
|---|---|---|
| SwiftUI | references/swiftui-patterns.md |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 构建视图、状态管理、修饰符 |
| 并发 | references/async-concurrency.md | async/await、actors、结构化并发 |
| 协议 | references/protocol-oriented.md | 协议设计、泛型、类型擦除 |
| 内存 | references/memory-performance.md | ARC、weak/unowned、性能优化 |
| 测试 | references/testing-patterns.md | XCTest、异步测试、模拟策略 |
// ✅ 正确做法:使用结构化错误处理的 async/await
func fetchUser(id: String) async throws -> User {
let url = URL(string: "https://api.example.com/users/\(id)")!
let (data, _) = try await URLSession.shared.data(from: url)
return try JSONDecoder().decode(User.self, from: data)
}
// ❌ 错误做法:在异步上下文中混用完成处理程序
func fetchUser(id: String) async throws -> User {
return try await withCheckedThrowingContinuation { continuation in
// 当存在原生异步版本时,避免以这种方式包装现有的异步 API
legacyFetch(id: id) { result in
continuation.resume(with: result)
}
}
}
// ✅ 正确做法:为视图模型使用 @Observable (Swift 5.9+)
@Observable
final class CounterViewModel {
var count = 0
func increment() { count += 1 }
}
struct CounterView: View {
@State private var vm = CounterViewModel()
var body: some View {
VStack {
Text("\(vm.count)")
Button("Increment", action: vm.increment)
}
}
}
// ❌ 错误做法:在 @Observable 足够时仍使用 ObservableObject/Published
class LegacyViewModel: ObservableObject {
@Published var count = 0 // 在 Swift 5.9+ 中是不必要的样板代码
}
// ✅ 正确做法:定义带有关联类型的能力协议
protocol Repository<Entity> {
associatedtype Entity: Identifiable
func fetch(id: Entity.ID) async throws -> Entity
func save(_ entity: Entity) async throws
}
struct UserRepository: Repository {
typealias Entity = User
func fetch(id: UUID) async throws -> User { /* … */ }
func save(_ user: User) async throws { /* … */ }
}
// ❌ 错误做法:在协议适用时使用类作为基类型
class BaseRepository { // 避免使用类继承来实现共享行为
func fetch(id: UUID) async throws -> Any { fatalError("Override required") }
}
// ✅ 正确做法:将可变共享状态隔离在 actor 中
actor ImageCache {
private var cache: [URL: UIImage] = [:]
func image(for url: URL) -> UIImage? { cache[url] }
func store(_ image: UIImage, for url: URL) { cache[url] = image }
}
// ❌ 错误做法:使用带有手动锁定的类
class UnsafeImageCache {
private var cache: [URL: UIImage] = [:]
private let lock = NSLock() // 容易出错;首选 actor 隔离
func image(for url: URL) -> UIImage? {
lock.lock(); defer { lock.unlock() }
return cache[url]
}
}
async/await(参见上面的模式)Sendable 合规性struct/enum)/// …) 为 API 编写文档!)在实现 Swift 功能时,请提供:
每周安装量
949
代码仓库
GitHub 星标
7.2K
首次出现
2026年1月21日
安全审计
安装于
opencode785
gemini-cli759
codex756
claude-code732
github-copilot693
cursor672
Validation checkpoints: After step 3, run
swift buildto verify compilation. After step 4, runswift build -warnings-as-errorsto surface actor isolation and Sendable warnings. After step 5, runswift testand confirm all async tests pass.
Load detailed guidance based on context:
| Topic | Reference | Load When |
|---|---|---|
| SwiftUI | references/swiftui-patterns.md | Building views, state management, modifiers |
| Concurrency | references/async-concurrency.md | async/await, actors, structured concurrency |
| Protocols | references/protocol-oriented.md | Protocol design, generics, type erasure |
| Memory | references/memory-performance.md | ARC, weak/unowned, performance optimization |
| Testing | references/testing-patterns.md | XCTest, async tests, mocking strategies |
// ✅ DO: async/await with structured error handling
func fetchUser(id: String) async throws -> User {
let url = URL(string: "https://api.example.com/users/\(id)")!
let (data, _) = try await URLSession.shared.data(from: url)
return try JSONDecoder().decode(User.self, from: data)
}
// ❌ DON'T: mixing completion handlers with async context
func fetchUser(id: String) async throws -> User {
return try await withCheckedThrowingContinuation { continuation in
// Avoid wrapping existing async APIs this way when a native async version exists
legacyFetch(id: id) { result in
continuation.resume(with: result)
}
}
}
// ✅ DO: use @Observable (Swift 5.9+) for view models
@Observable
final class CounterViewModel {
var count = 0
func increment() { count += 1 }
}
struct CounterView: View {
@State private var vm = CounterViewModel()
var body: some View {
VStack {
Text("\(vm.count)")
Button("Increment", action: vm.increment)
}
}
}
// ❌ DON'T: reach for ObservableObject/Published when @Observable suffices
class LegacyViewModel: ObservableObject {
@Published var count = 0 // Unnecessary boilerplate in Swift 5.9+
}
// ✅ DO: define capability protocols with associated types
protocol Repository<Entity> {
associatedtype Entity: Identifiable
func fetch(id: Entity.ID) async throws -> Entity
func save(_ entity: Entity) async throws
}
struct UserRepository: Repository {
typealias Entity = User
func fetch(id: UUID) async throws -> User { /* … */ }
func save(_ user: User) async throws { /* … */ }
}
// ❌ DON'T: use classes as base types when a protocol fits
class BaseRepository { // Avoid class inheritance for shared behavior
func fetch(id: UUID) async throws -> Any { fatalError("Override required") }
}
// ✅ DO: isolate mutable shared state in an actor
actor ImageCache {
private var cache: [URL: UIImage] = [:]
func image(for url: URL) -> UIImage? { cache[url] }
func store(_ image: UIImage, for url: URL) { cache[url] = image }
}
// ❌ DON'T: use a class with manual locking
class UnsafeImageCache {
private var cache: [URL: UIImage] = [:]
private let lock = NSLock() // Error-prone; prefer actor isolation
func image(for url: URL) -> UIImage? {
lock.lock(); defer { lock.unlock() }
return cache[url]
}
}
async/await for asynchronous operations (see pattern above)Sendable compliance for concurrencystruct/enum) by default/// …)!) without justificationWhen implementing Swift features, provide:
Weekly Installs
949
Repository
GitHub Stars
7.2K
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode785
gemini-cli759
codex756
claude-code732
github-copilot693
cursor672
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
Grimoire CLI 使用指南:区块链法术编写、验证与执行全流程
940 周安装
Grimoire Uniswap 技能:查询 Uniswap 元数据与生成代币/资金池快照的 CLI 工具
940 周安装
Grimoire Aave 技能:查询 Aave V3 元数据和储备快照的 CLI 工具
941 周安装
Railway CLI 部署指南:使用 railway up 命令快速部署代码到 Railway 平台
942 周安装
n8n Python 代码节点使用指南:在自动化工作流中编写 Python 脚本
943 周安装
Flutter Platform Views 实现指南:Android/iOS/macOS原生视图与Web嵌入教程
943 周安装