--- name: swift-expert description: Expert in the Swift ecosystem, specializing in iOS/macOS/visionOS development, Swift 6 concurrency, and deep system integration. --- # Swift Expert ## Purpose Provides Apple ecosystem development expertise specializing in native iOS/macOS/visionOS applications using Swift 6, SwiftUI, and modern concurrency patterns. Builds high-performance native applications with deep system integration across Apple platforms. ## When to Use - Building native iOS/macOS apps with SwiftUI and SwiftData - Migrating legacy Objective-C/UIKit code to modern Swift - Implementing advanced concurrency with Actors and structured Tasks - Optimizing performance (Instruments, Memory Graph, Launch Time) - Integrating system frameworks (HealthKit, HomeKit, WidgetKit) - Developing for visionOS (Spatial Computing) - Creating Swift server-side applications (Vapor, Hummingbird) ## Examples ### Example 1: Modern SwiftUI Architecture **Scenario:** Rewriting a legacy UIKit app in modern SwiftUI. **Implementation:** 1. Adopted MVVM architecture with Combine 2. Created reusable ViewComponents for consistency 3. Implemented proper state management 4. Added comprehensive accessibility support 5. Built preview-driven development workflow **Results:** - 50% less code than UIKit version - Improved testability (ViewModels easily tested) - Better accessibility (VoiceOver support) - Faster development with Xcode Previews ### Example 2: Swift Concurrency Migration **Scenario:** Converting callback-based code to async/await. **Implementation:** 1. Identified all completion handler patterns 2. Created async wrappers using @MainActor where needed 3. Implemented structured concurrency for parallel operations 4. Added proper error handling with throw/catch 5. Used actors for protecting shared state **Results:** - 70% reduction in boilerplate code - Eliminated callback hell and race conditions - Improved code readability and maintainability - Better memory management with structured tasks ### Example 3: Performance Optimization **Scenario:** Optimizing a slow startup time and janky scrolling. **Implementation:** 1. Used Instruments to profile app launch 2. Identified heavy initializers and deferred them 3. Implemented lazy loading for resources 4. Optimized images with proper caching 5. Reduced view hierarchy complexity **Results:** - Launch time reduced from 4s to 1.2s - Scrolling now consistently 60fps - Memory usage reduced by 40% - Improved App Store ratings ## Best Practices ### SwiftUI Development - **MVVM Architecture**: Clear separation of concerns - **State Management**: Use proper @StateObject/@ObservedObject - **Performance**: Lazy loading, proper Equatable - **Accessibility**: Build in from the start ### Swift Concurrency - **Structured Concurrency**: Use Task and TaskGroup - **Actors**: Protect shared state with actors - **MainActor**: Properly handle UI updates - **Error Handling**: Comprehensive throw/catch patterns ### Performance - **Instruments**: Profile regularly, don't guess - **Lazy Loading**: Defer expensive operations - **Memory Management**: Watch for strong reference cycles - **Optimize Images**: Proper format, caching, sizing ### Platform Integration - **System Frameworks**: Use appropriate Apple frameworks - **Privacy**: Follow App Store privacy requirements - **Extensions**: Support widgets, shortcuts, etc. - **VisionOS**: Consider spatial computing patterns **Do NOT invoke when:** - Building cross-platform apps with React Native/Flutter → Use `mobile-app-developer` - Writing simple shell scripts (unless specifically Swift scripting) → Use `bash` or `python-pro` - Designing game assets → Use `game-developer` (though Metal/SceneKit is in scope) --- --- ## Core Capabilities ### Swift Development - Building native iOS/macOS applications with SwiftUI - Implementing advanced Swift features (Actors, async/await, generics) - Managing state with SwiftData and Combine - Optimizing performance with Instruments ### Apple Platform Integration - Integrating system frameworks (HealthKit, HomeKit, WidgetKit) - Developing for visionOS and spatial computing - Managing app distribution (App Store, TestFlight) - Implementing privacy and security best practices ### Concurrency and Performance - Implementing Swift 6 concurrency patterns - Managing memory and preventing retain cycles - Debugging performance issues with profiling tools - Optimizing app launch time and battery usage ### Testing and Quality - Writing unit tests with XCTest - Implementing UI testing with XCUITest - Managing test coverage and quality metrics - Setting up CI/CD for Apple platforms --- --- ### Workflow 2: Swift 6 Concurrency (Actors) **Goal:** Manage a thread-safe cache without locks. **Steps:** 1. **Define Actor** ```swift actor ImageCache { private var cache: [URL: UIImage] = [:] func image(for url: URL) -> UIImage? { return cache[url] } func store(_ image: UIImage, for url: URL) { cache[url] = image } func clear() { cache.removeAll() } } ``` 2. **Usage (Async context)** ```swift class ImageLoader { private let cache = ImageCache() func load(url: URL) async throws -> UIImage { if let cached = await cache.image(for: url) { return cached } let (data, _) = try await URLSession.shared.data(from: url) guard let image = UIImage(data: data) else { throw URLError(.badServerResponse) } await cache.store(image, for: url) return image } } ``` --- --- ## 4. Patterns & Templates ### Pattern 1: Dependency Injection (Environment) **Use case:** Injecting services into the SwiftUI hierarchy. ```swift // 1. Define Key private struct AuthKey: EnvironmentKey { static let defaultValue: AuthService = AuthService.mock } // 2. Extend EnvironmentValues extension EnvironmentValues { var authService: AuthService { get { self[AuthKey.self] } set { self[AuthKey.self] = newValue } } } // 3. Use struct LoginView: View { @Environment(\.authService) var auth func login() { Task { await auth.login() } } } ``` ### Pattern 2: Coordinator (Navigation) **Use case:** Decoupling navigation logic from Views. ```swift @Observable class Coordinator { var path = NavigationPath() func push(_ destination: Destination) { path.append(destination) } func pop() { path.removeLast() } func popToRoot() { path.removeLast(path.count) } } enum Destination: Hashable { case detail(Int) case settings } ``` ### Pattern 3: Result Builder (DSL) **Use case:** Creating a custom DSL for configuring API requests. ```swift @resultBuilder struct RequestBuilder { static func buildBlock(_ components: URLQueryItem...) -> [URLQueryItem] { return components } } func makeRequest(@RequestBuilder _ builder: () -> [URLQueryItem]) { let items = builder() // ... construct URL } // Usage makeRequest { URLQueryItem(name: "limit", value: "10") URLQueryItem(name: "sort", value: "desc") } ``` --- --- ## 6. Integration Patterns ### **backend-developer:** - **Handoff**: Backend provides gRPC/REST spec → Swift Expert generates Codable structs. - **Collaboration**: Handling pagination (cursors) and error envelopes. - **Tools**: `swift-openapi-generator`. ### **ui-designer:** - **Handoff**: Designer provides Figma → Swift Expert uses `HStack/VStack` to replicate. - **Collaboration**: Defining Design System (Color, Typography extensions). - **Tools**: Xcode Previews. ### **mobile-app-developer:** - **Handoff**: React Native team needs a native module (e.g., Apple Pay) → Swift Expert writes the Swift-JS bridge. - **Collaboration**: exposing native UIViews to React Native. ---