{"id":"swift-expert","name":"swift-expert","summary":"iOS/macOS/watchOS/tvOSアプリケーションを構築し、SwiftUIのビューや状態管理を実装し、プロトコル指向アーキテクチャを設計し、非同期/待機の並行処理を担当し、スレッドセーフティのためのアクターを実装し、Swift特有の問題をデバッグします。","body":"# Swift Expert\n\n## Core Workflow\n\n1. **Architecture Analysis** - Identify platform targets, dependencies, design patterns\n2. **Design Protocols** - Create protocol-first APIs with associated types\n3. **Implement** - Write type-safe code with async/await and value semantics\n4. **Optimize** - Profile with Instruments, ensure thread safety\n5. **Test** - Write comprehensive tests with XCTest and async patterns\n\n> **Validation checkpoints:** After step 3, run `swift build` to verify compilation. After step 4, run `swift build -warnings-as-errors` to surface actor isolation and Sendable warnings. After step 5, run `swift test` and confirm all async tests pass.\n\n## Reference Guide\n\nLoad detailed guidance based on context:\n\n| Topic | Reference | Load When |\n|-------|-----------|-----------|\n| SwiftUI | `references/swiftui-patterns.md` | Building views, state management, modifiers |\n| Concurrency | `references/async-concurrency.md` | async/await, actors, structured concurrency |\n| Protocols | `references/protocol-oriented.md` | Protocol design, generics, type erasure |\n| Memory | `references/memory-performance.md` | ARC, weak/unowned, performance optimization |\n| Testing | `references/testing-patterns.md` | XCTest, async tests, mocking strategies |\n\n## Code Patterns\n\n### async/await — Correct vs. Incorrect\n\n```swift\n// ✅ DO: async/await with structured error handling\nfunc fetchUser(id: String) async throws -> User {\n    let url = URL(string: \"https://api.example.com/users/\\(id)\")!\n    let (data, _) = try await URLSession.shared.data(from: url)\n    return try JSONDecoder().decode(User.self, from: data)\n}\n\n// ❌ DON'T: mixing completion handlers with async context\nfunc fetchUser(id: String) async throws -> User {\n    return try await withCheckedThrowingContinuation { continuation in\n        // Avoid wrapping existing async APIs this way when a native async version exists\n        legacyFetch(id: id) { result in\n            continuation.resume(with: result)\n        }\n    }\n}\n```\n\n### SwiftUI State Management\n\n```swift\n// ✅ DO: use @Observable (Swift 5.9+) for view models\n@Observable\nfinal class CounterViewModel {\n    var count = 0\n    func increment() { count += 1 }\n}\n\nstruct CounterView: View {\n    @State private var vm = CounterViewModel()\n\n    var body: some View {\n        VStack {\n            Text(\"\\(vm.count)\")\n            Button(\"Increment\", action: vm.increment)\n        }\n    }\n}\n\n// ❌ DON'T: reach for ObservableObject/Published when @Observable suffices\nclass LegacyViewModel: ObservableObject {\n    @Published var count = 0  // Unnecessary boilerplate in Swift 5.9+\n}\n```\n\n### Protocol-Oriented Architecture\n\n```swift\n// ✅ DO: define capability protocols with associated types\nprotocol Repository<Entity> {\n    associatedtype Entity: Identifiable\n    func fetch(id: Entity.ID) async throws -> Entity\n    func save(_ entity: Entity) async throws\n}\n\nstruct UserRepository: Repository {\n    typealias Entity = User\n    func fetch(id: UUID) async throws -> User { /* … */ }\n    func save(_ user: User) async throws { /* … */ }\n}\n\n// ❌ DON'T: use classes as base types when a protocol fits\nclass BaseRepository {  // Avoid class inheritance for shared behavior\n    func fetch(id: UUID) async throws -> Any { fatalError(\"Override required\") }\n}\n```\n\n### Actor for Thread Safety\n\n```swift\n// ✅ DO: isolate mutable shared state in an actor\nactor ImageCache {\n    private var cache: [URL: UIImage] = [:]\n\n    func image(for url: URL) -> UIImage? { cache[url] }\n    func store(_ image: UIImage, for url: URL) { cache[url] = image }\n}\n\n// ❌ DON'T: use a class with manual locking\nclass UnsafeImageCache {\n    private var cache: [URL: UIImage] = [:]\n    private let lock = NSLock()  // Error-prone; prefer actor isolation\n    func image(for url: URL) -> UIImage? {\n        lock.lock(); defer { lock.unlock() }\n        return cache[url]\n    }\n}\n```\n\n## Constraints\n\n### MUST DO\n- Use type hints and inference appropriately\n- Follow Swift API Design Guidelines\n- Use `async/await` for asynchronous operations (see pattern above)\n- Ensure `Sendable` compliance for concurrency\n- Use value types (`struct`/`enum`) by default\n- Document APIs with markup comments (`/// …`)\n- Use property wrappers for cross-cutting concerns\n- Profile with Instruments before optimizing\n\n### MUST NOT DO\n- Use force unwrapping (`!`) without justification\n- Create retain cycles in closures\n- Mix synchronous and asynchronous code improperly\n- Ignore actor isolation warnings\n- Use implicitly unwrapped optionals unnecessarily\n- Skip error handling\n- Use Objective-C patterns when Swift alternatives exist\n- Hardcode platform-specific values\n\n## Output Templates\n\nWhen implementing Swift features, provide:\n1. Protocol definitions and type aliases\n2. Model types (structs/classes with value semantics)\n3. View implementations (SwiftUI) or view controllers\n4. Tests demonstrating usage\n5. Brief explanation of architectural decisions\n\n[Documentation](https://jeffallan.github.io/claude-skills/skills/language/swift-expert/)","author":"@Jeffallan","ownerProfile":null,"authorContacts":null,"sourceUrl":"https://github.com/Jeffallan/claude-skills/tree/main/skills/swift-expert","license":"MIT","category":"writing","lang":"en","tokens":1149,"stars":0,"calls30d":2,"claimed":false,"visibility":"public","origin":"crawler","version":"0.1.0","createdAt":"2026-08-22","updatedAt":"2026-08-22","files":[{"path":"references/async-concurrency.md","size":8467,"sha256":"7555824cab883df018120a81c944cec624172479e43761dd0d4920d98dbdca9c"},{"path":"references/memory-performance.md","size":8256,"sha256":"1b7c985aa18642af98f348d836e78d85acf4493bebe6d669b6e2171710377ddc"},{"path":"references/protocol-oriented.md","size":7360,"sha256":"f8be1afddc5669b0a9f4490cfc4f970e0f0f88e7de9688acc65fbbc6a1b2d1dc"},{"path":"references/swiftui-patterns.md","size":6806,"sha256":"0bb827a095bb753ab50c0be9c2ed08b274dbfd2797af447133cd1d2b8bb0b431"},{"path":"references/testing-patterns.md","size":9056,"sha256":"1893500e5159de0c777ca0396fd21c031b3eec7a4e96123811d4fcbc3653b220"}],"requires":{"mcp":[],"tools":[]},"safety":{"flags":[],"scannedAt":"2026-08-22","hasScripts":false,"networkEndpoints":["api.example.com","jeffallan.github.io"]}}