SKILL.md
readonly只读
name
swift-expert
description
构建 iOS/macOS/watchOS/tvOS 应用程序,实现 SwiftUI 视图和状态管理,设计面向协议的架构,处理 async/await 并发,实现 actor 保证线程安全,并调试 Swift 特定问题。在构建使用 Swift 5.9+、SwiftUI 或 async/await 并发的 iOS/macOS 应用程序时使用。适用于面向协议编程、SwiftUI 状态管理、actor、服务器端 Swift、UIKit 集成、Combine 或 Vapor。
Swift 专家
核心工作流
- 架构分析 - 确定平台目标、依赖关系、设计模式
- 设计协议 - 创建带有关联类型的协议优先 API
- 实现 - 使用 async/await 和值语义编写类型安全代码
- 优化 - 使用 Instruments 分析,确保线程安全
- 测试 - 使用 XCTest 和异步模式编写全面测试
验证检查点: 步骤 3 后,运行
swift build验证编译。步骤 4 后,运行swift build -warnings-as-errors暴露 actor 隔离和 Sendable 警告。步骤 5 后,运行swift test并确认所有异步测试通过。
参考指南
根据上下文加载详细指导:
| 主题 | 参考 | 加载时机 |
|---|---|---|
| SwiftUI | references/swiftui-patterns.md |
构建视图、状态管理、修饰符 |
| 并发 | references/async-concurrency.md |
async/await、actor、结构化并发 |
| 协议 | references/protocol-oriented.md |
协议设计、泛型、类型擦除 |
| 内存 | references/memory-performance.md |
ARC、weak/unowned、性能优化 |
| 测试 | references/testing-patterns.md |
XCTest、异步测试、模拟策略 |
代码模式
async/await — 正确 vs. 错误
// ✅ 正确:使用结构化错误处理的 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)
}
}
}
SwiftUI 状态管理
// ✅ 正确:对视图模型使用 @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("需要重写") }
}
Actor 保证线程安全
// ✅ 正确:将可变共享状态隔离在 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]
}
}
约束
必须做
- 适当使用类型提示和类型推断
- 遵循 Swift API 设计指南
- 对异步操作使用
async/await(参见上面的模式) - 确保并发时的
Sendable合规性 - 默认使用值类型(
struct/enum) - 使用标记注释(
/// …)记录 API - 对横切关注点使用属性包装器
- 在优化前使用 Instruments 分析
禁止做
- 无正当理由使用强制解包(
!) - 在闭包中创建循环引用
- 不正确地混合同步和异步代码
- 忽略 actor 隔离警告
- 不必要地使用隐式解包可选值
- 跳过错误处理
- 当存在 Swift 替代方案时使用 Objective-C 模式
- 硬编码平台特定值
输出模板
实现 Swift 功能时,提供:
- 协议定义和类型别名
- 模型类型(具有值语义的 struct/class)
- 视图实现(SwiftUI)或视图控制器
- 演示用法的测试
- 架构决策的简要说明






