swift-patterns
npx machina-cli add skill shahtuyakov/claude-setup/swift-patterns --openclawFiles (1)
SKILL.md
4.5 KB
Swift Patterns
Modern Swift and SwiftUI patterns for iOS development.
iOS Version Selection
| Target | Best For | Stack |
|---|---|---|
| iOS 26+ | New apps, latest features | SwiftUI + SwiftData + @Observable + Foundation Models |
| iOS 17+ | Broad compatibility with modern APIs | SwiftUI + SwiftData + @Observable |
| iOS 15+ | Maximum device coverage | SwiftUI + Core Data + ObservableObject |
Reference Files
| Topic | Load | Use When |
|---|---|---|
| Project structure | references/project-structure.md | Setting up or organizing code |
| SwiftUI views | references/swiftui-patterns.md | Building UI components |
| State management | references/state-management.md | @Observable, ObservableObject, @State |
| Data persistence | references/data-persistence.md | SwiftData, Core Data, Keychain |
| Networking | references/networking.md | URLSession, API clients |
| Concurrency | references/concurrency.md | async/await, actors, tasks |
| Navigation | references/navigation.md | NavigationStack, coordinators |
| iOS 26 features | references/ios26-features.md | Liquid Glass, Foundation Models |
Quick Start Patterns
Basic SwiftUI View (iOS 17+)
import SwiftUI
struct UserProfileView: View {
@State private var viewModel = UserProfileViewModel()
var body: some View {
Group {
if viewModel.isLoading {
ProgressView()
} else if let user = viewModel.user {
VStack {
Text(user.name)
.font(.title)
Text(user.email)
.foregroundStyle(.secondary)
}
} else if let error = viewModel.error {
ContentUnavailableView(
"Error",
systemImage: "exclamationmark.triangle",
description: Text(error.localizedDescription)
)
}
}
.task {
await viewModel.loadUser()
}
}
}
Observable ViewModel (iOS 17+)
import Foundation
@Observable
class UserProfileViewModel {
var user: User?
var isLoading = false
var error: Error?
func loadUser() async {
isLoading = true
defer { isLoading = false }
do {
user = try await APIClient.shared.fetchCurrentUser()
} catch {
self.error = error
}
}
}
SwiftData Model (iOS 17+)
import SwiftData
@Model
class User {
var id: UUID
var name: String
var email: String
var createdAt: Date
init(id: UUID = UUID(), name: String, email: String) {
self.id = id
self.name = name
self.email = email
self.createdAt = Date()
}
}
Async API Call
func fetchUsers() async throws -> [User] {
let url = URL(string: "https://api.example.com/users")!
let (data, response) = try await URLSession.shared.data(from: url)
guard let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200 else {
throw APIError.invalidResponse
}
return try JSONDecoder().decode([User].self, from: data)
}
Architecture Patterns
| Pattern | Best For | Complexity |
|---|---|---|
| MVVM | Most apps | Medium |
| TCA | Large teams, complex state | High |
| Coordinator | Complex navigation | Medium |
Essential Packages
| Package | Purpose |
|---|---|
| SwiftData | Persistence (iOS 17+) |
| SwiftUI | UI framework |
| Foundation | Networking, dates, etc. |
| Swift Testing | Modern testing |
| KeychainAccess | Secure storage |
Swift 6.2 Key Features
- Approachable Concurrency - Main thread by default
- @concurrent - Explicit background execution
- Typed throws - Better error handling
- @Animatable macro - Simplified animations
Code Quality Rules
- @Observable over ObservableObject - For iOS 17+
- Structured concurrency - Use async/await, not callbacks
- SwiftData over Core Data - For iOS 17+ new projects
- Swift Testing over XCTest - For new test code
- SPM over CocoaPods - For dependency management
- Value types - Prefer structs over classes when possible
Source
git clone https://github.com/shahtuyakov/claude-setup/blob/main/skills/swift-patterns/SKILL.mdView on GitHub Overview
Swift Patterns introduces modern Swift and SwiftUI approaches for building native iOS/iPadOS apps. It covers patterns for views, view models, data persistence, networking, concurrency, and navigation, aligned with Swift 6.2, SwiftData, and iOS 26 features such as Liquid Glass and Foundation Models.
How This Skill Works
Patterns are organized by topic with version-aware guidance (iOS 26+, iOS 17+, iOS 15+). The guide provides concrete Quick Start patterns, architecture options like MVVM, TCA, and Coordinators, and references to essential packages (SwiftData, Foundation, SwiftUI) to implement end-to-end flows.
When to Use It
- Starting a new app on iOS 26+ leveraging SwiftUI, SwiftData, and Foundation Models
- Building reusable SwiftUI views and state management with @Observable
- Implementing data persistence with SwiftData or Core Data
- Creating network clients using URLSession and async/await
- Designing navigation flows with NavigationStack and coordinators
Quick Start
- Step 1: Choose your iOS target (iOS 26+ for latest features or 17+/15+ for broader compatibility)
- Step 2: Create a Basic SwiftUI View paired with an Observable ViewModel (iOS 17+)
- Step 3: Add a SwiftData model and implement a sample Async API call pattern
Best Practices
- Choose MVVM for most apps; consider TCA for large teams or complex state
- Leverage @Observable and SwiftData for modern state management and persistence
- Keep UI code modular with isolated SwiftUI views and view models
- Use a Coordinator pattern or NavigationStack to manage robust navigation
- Adopt async/await and actors for concurrency and testable asynchronous code
Example Use Cases
- User profile screen implemented with a Basic SwiftUI View and an Observable ViewModel
- Async API call pattern to fetchUsers using URLSession and async/await
- SwiftData model example for User with id, name, email, and createdAt
- Loading and error handling in a SwiftUI view using isLoading and error states
- Architecture-minded skeleton showing MVVM with a dedicated APIClient and navigation flow
Frequently Asked Questions
Add this skill to your agents