
使用方式
关于
构建 iOS/macOS/watchOS/tvOS 应用,实现 SwiftUI 视图和状态管理,设计面向协议的架构,处理 async/await 并发,实现 Actor 保证线程安全,调试 Swift 特定问题。适用于使用 Swift 5.9+ 和 SwiftUI 构建 iOS/macOS 应用。
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、actors、结构化并发 |
| 协议 | references/protocol-oriented.md | 协议设计、泛型、类型擦除 |
| 内存 | references/memory-performance.md | ARC、weak/unowned、性能优化 |
| 测试 | references/testing-patterns.md | XCTest、异步测试、Mock 策略 |
代码模式
async/await — 正确 vs. 错误
// ✅ 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)
}
}
}
SwiftUI 状态管理
// ✅ 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") }
}
使用 Actor 确保线程安全
// ✅ 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]
}
}
约束条件
必须做到
- 适当使用类型提示和类型推断
- 遵循 Swift API 设计指南
- 对异步操作使用
async/await(参见上方模式) - 确保并发的
Sendable合规性 - 默认使用值类型(
struct/enum) - 使用标记注释记录 API(
/// …) - 使用属性包装器处理横切关注点
- 优化前先使用 Instruments 进行性能分析
禁止事项
- 无正当理由使用强制解包(
!) - 在闭包中创建循环引用
- 不当混合同步和异步代码
- 忽略 actor 隔离警告
- 不必要地使用隐式解包可选值
- 跳过错误处理
- 存在 Swift 替代方案时使用 Objective-C 模式
- 硬编码平台特定值
输出模板
实现 Swift 功能时,提供:
- 协议定义和类型别名
- 模型类型(具有值语义的 struct/class)
- 视图实现(SwiftUI)或视图控制器
- 演示用法的测试
- 架构决策的简要说明
兼容工具
Claude CodeCursor
标签
移动端
