swift-expert
它能做什么
它指导 AI 为苹果平台编写 Swift 应用,包括 SwiftUI 界面和安全的并发处理。适用于开发苹果应用的开发者。
适用场景
- ✓当你用 SwiftUI 构建 iPhone 或 Mac 应用时。
- ✓当你的 Swift 应用需要异步代码、actor 或线程安全的数据时。
- ✓当你想为 Swift 项目编写 XCTest 测试时。
技能链接
https://github.com/Jeffallan/claude-skills/blob/main/skills/swift-expert/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我构建一个 SwiftUI 界面,安全地从网络加载数据。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会规划协议和结构,编写 Swift 代码,然后构建并运行测试,以发现错误和并发警告。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Swift Expert
Core Workflow
- Architecture Analysis - Identify platform targets, dependencies, design patterns
- Design Protocols - Create protocol-first APIs with associated types
- Implement - Write type-safe code with async/await and value semantics
- Optimize - Profile with Instruments, ensure thread safety
- Test - Write comprehensive tests with XCTest and async patterns
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.
Reference Guide
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 |
Code Patterns
async/await — Correct vs. Incorrect
// ✅ 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 State Management
// ✅ 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+
}
Protocol-Oriented Architecture
// ✅ 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 for Thread Safety
// ✅ 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]
}
}
Constraints
MUST DO
- Use type hints and inference appropriately
- Follow Swift API Design Guidelines
- Use
async/awaitfor asynchronous operations (see pattern above) - Ensure
Sendablecompliance for concurrency - Use value types (
struct/enum) by default - Document APIs with markup comments (
/// …) - Use property wrappers for cross-cutting concerns
- Profile with Instruments before optimizing
MUST NOT DO
- Use force unwrapping (
!) without justification - Create retain cycles in closures
- Mix synchronous and asynchronous code improperly
- Ignore actor isolation warnings
- Use implicitly unwr
常见问题
它针对哪个 Swift 版本?
该技能提到 Swift 5.9 及更新版本,适合现代的 Swift 项目。
它涵盖手表和电视应用吗?
涵盖。它提到了 watchOS 和 tvOS,以及 iOS 和 macOS。
它会检查什么?
它运行构建和测试命令,并检查 actor 隔离和 Sendable 警告。
详情
作者的原始描述
Builds iOS/macOS/watchOS/tvOS applications, implements SwiftUI views and state management, designs protocol-oriented architectures, handles async/await concurrency, implements actors for thread safety, and debugs Swift-specific issues. Use when building iOS/macOS applications with Swift 5.9+, SwiftUI, or async/await concurrency. Invoke for protocol-oriented programming, SwiftUI state management, actors, server-side Swift, UIKit integration, Combine, or Vapor.
repo: Jeffallan/claude-skills
热度
- GitHub 星标
- 12k
- 排名
- #11462
- 许可证
- MIT
- 更新时间
- 2026/10/30
评论
- …
同一仓库的更多技能
angular-architect
12k帮你的智能体构建 Angular 17 及更新版本的应用,包括独立组件、懒加载路由、NgRx 状态管理、RxJS 模式和性能优化。
api-designer
12k帮你的智能体设计 REST 或 GraphQL API,并编写 OpenAPI 3.1 规范,包括资源、分页、错误格式和版本控制规则。
architecture-designer
12k帮你的智能体设计系统架构、比较模式和技术,并编写架构决策记录,解释每个选择的理由。
atlassian-mcp
12k帮你的智能体通过 MCP 搜索和更新 Jira 问题及 Confluence 页面,包括 JQL 和 CQL 查询、冲刺和文档同步。