Get the FREE Ultimate OpenClaw Setup Guide →

swift-patterns

npx machina-cli add skill shahtuyakov/claude-setup/swift-patterns --openclaw
Files (1)
SKILL.md
4.5 KB

Swift Patterns

Modern Swift and SwiftUI patterns for iOS development.

iOS Version Selection

TargetBest ForStack
iOS 26+New apps, latest featuresSwiftUI + SwiftData + @Observable + Foundation Models
iOS 17+Broad compatibility with modern APIsSwiftUI + SwiftData + @Observable
iOS 15+Maximum device coverageSwiftUI + Core Data + ObservableObject

Reference Files

TopicLoadUse When
Project structurereferences/project-structure.mdSetting up or organizing code
SwiftUI viewsreferences/swiftui-patterns.mdBuilding UI components
State managementreferences/state-management.md@Observable, ObservableObject, @State
Data persistencereferences/data-persistence.mdSwiftData, Core Data, Keychain
Networkingreferences/networking.mdURLSession, API clients
Concurrencyreferences/concurrency.mdasync/await, actors, tasks
Navigationreferences/navigation.mdNavigationStack, coordinators
iOS 26 featuresreferences/ios26-features.mdLiquid 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

PatternBest ForComplexity
MVVMMost appsMedium
TCALarge teams, complex stateHigh
CoordinatorComplex navigationMedium

Essential Packages

PackagePurpose
SwiftDataPersistence (iOS 17+)
SwiftUIUI framework
FoundationNetworking, dates, etc.
Swift TestingModern testing
KeychainAccessSecure storage

Swift 6.2 Key Features

  1. Approachable Concurrency - Main thread by default
  2. @concurrent - Explicit background execution
  3. Typed throws - Better error handling
  4. @Animatable macro - Simplified animations

Code Quality Rules

  1. @Observable over ObservableObject - For iOS 17+
  2. Structured concurrency - Use async/await, not callbacks
  3. SwiftData over Core Data - For iOS 17+ new projects
  4. Swift Testing over XCTest - For new test code
  5. SPM over CocoaPods - For dependency management
  6. 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

  1. Step 1: Choose your iOS target (iOS 26+ for latest features or 17+/15+ for broader compatibility)
  2. Step 2: Create a Basic SwiftUI View paired with an Observable ViewModel (iOS 17+)
  3. 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
Sponsor this space

Reach thousands of developers