$npx -y skills add Jeffallan/claude-skills --skill swift-expertBuilds 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 wit
| 1 | # Swift Expert |
| 2 | |
| 3 | ## Core Workflow |
| 4 | |
| 5 | 1. **Architecture Analysis** - Identify platform targets, dependencies, design patterns |
| 6 | 2. **Design Protocols** - Create protocol-first APIs with associated types |
| 7 | 3. **Implement** - Write type-safe code with async/await and value semantics |
| 8 | 4. **Optimize** - Profile with Instruments, ensure thread safety |
| 9 | 5. **Test** - Write comprehensive tests with XCTest and async patterns |
| 10 | |
| 11 | > **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. |
| 12 | |
| 13 | ## Reference Guide |
| 14 | |
| 15 | Load detailed guidance based on context: |
| 16 | |
| 17 | | Topic | Reference | Load When | |
| 18 | |-------|-----------|-----------| |
| 19 | | SwiftUI | `references/swiftui-patterns.md` | Building views, state management, modifiers | |
| 20 | | Concurrency | `references/async-concurrency.md` | async/await, actors, structured concurrency | |
| 21 | | Protocols | `references/protocol-oriented.md` | Protocol design, generics, type erasure | |
| 22 | | Memory | `references/memory-performance.md` | ARC, weak/unowned, performance optimization | |
| 23 | | Testing | `references/testing-patterns.md` | XCTest, async tests, mocking strategies | |
| 24 | |
| 25 | ## Code Patterns |
| 26 | |
| 27 | ### async/await — Correct vs. Incorrect |
| 28 | |
| 29 | ```swift |
| 30 | // ✅ DO: async/await with structured error handling |
| 31 | func fetchUser(id: String) async throws -> User { |
| 32 | let url = URL(string: "https://api.example.com/users/\(id)")! |
| 33 | let (data, _) = try await URLSession.shared.data(from: url) |
| 34 | return try JSONDecoder().decode(User.self, from: data) |
| 35 | } |
| 36 | |
| 37 | // ❌ DON'T: mixing completion handlers with async context |
| 38 | func fetchUser(id: String) async throws -> User { |
| 39 | return try await withCheckedThrowingContinuation { continuation in |
| 40 | // Avoid wrapping existing async APIs this way when a native async version exists |
| 41 | legacyFetch(id: id) { result in |
| 42 | continuation.resume(with: result) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | ``` |
| 47 | |
| 48 | ### SwiftUI State Management |
| 49 | |
| 50 | ```swift |
| 51 | // ✅ DO: use @Observable (Swift 5.9+) for view models |
| 52 | @Observable |
| 53 | final class CounterViewModel { |
| 54 | var count = 0 |
| 55 | func increment() { count += 1 } |
| 56 | } |
| 57 | |
| 58 | struct CounterView: View { |
| 59 | @State private var vm = CounterViewModel() |
| 60 | |
| 61 | var body: some View { |
| 62 | VStack { |
| 63 | Text("\(vm.count)") |
| 64 | Button("Increment", action: vm.increment) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // ❌ DON'T: reach for ObservableObject/Published when @Observable suffices |
| 70 | class LegacyViewModel: ObservableObject { |
| 71 | @Published var count = 0 // Unnecessary boilerplate in Swift 5.9+ |
| 72 | } |
| 73 | ``` |
| 74 | |
| 75 | ### Protocol-Oriented Architecture |
| 76 | |
| 77 | ```swift |
| 78 | // ✅ DO: define capability protocols with associated types |
| 79 | protocol Repository<Entity> { |
| 80 | associatedtype Entity: Identifiable |
| 81 | func fetch(id: Entity.ID) async throws -> Entity |
| 82 | func save(_ entity: Entity) async throws |
| 83 | } |
| 84 | |
| 85 | struct UserRepository: Repository { |
| 86 | typealias Entity = User |
| 87 | func fetch(id: UUID) async throws -> User { /* … */ } |
| 88 | func save(_ user: User) async throws { /* … */ } |
| 89 | } |
| 90 | |
| 91 | // ❌ DON'T: use classes as base types when a protocol fits |
| 92 | class BaseRepository { // Avoid class inheritance for shared behavior |
| 93 | func fetch(id: UUID) async throws -> Any { fatalError("Override required") } |
| 94 | } |
| 95 | ``` |
| 96 | |
| 97 | ### Actor for Thread Safety |
| 98 | |
| 99 | ```swift |
| 100 | // ✅ DO: isolate mutable shared state in an actor |
| 101 | actor ImageCache { |
| 102 | private var cache: [URL: UIImage] = [:] |
| 103 | |
| 104 | func image(for url: URL) -> UIImage? { cache[url] } |
| 105 | func store(_ image: UIImage, for url: URL) { cache[url] = image } |
| 106 | } |
| 107 | |
| 108 | // ❌ DON'T: use a class with manual locking |
| 109 | class UnsafeImageCache { |
| 110 | private var cache: [URL: UIImage] = [:] |
| 111 | private let lock = NSLock() // Error-prone; prefer actor isolation |
| 112 | func image(for url: URL) -> UIImage? { |
| 113 | lock.lock(); defer { lock.unlock() } |
| 114 | return cache[url] |
| 115 | } |
| 116 | } |
| 117 | ``` |
| 118 | |
| 119 | ## Constraints |
| 120 | |
| 121 | ### MUST DO |
| 122 | - Use type hints and inference appropriately |
| 123 | - Follow Swift API Design Guidelines |
| 124 | - Use `async/await` for asynchronous operations (see pattern above) |
| 125 | - Ensure `Sendable` compliance for concurrency |
| 126 | - Use value types (`struct`/`enum`) by default |
| 127 | - Document APIs with markup comments (`/// …`) |
| 128 | - Use prope |