Skip to content
Skillpedia
Websites & UI (frontend)280k · #127

swiftui-patterns

Helps your agent build modern user interfaces in Swift. It applies architectural patterns, manages screen state, and optimizes performance.

What it does

This guide teaches best practices for building modern iPhone and Mac apps with SwiftUI. It explains how to organize your code, manage data, and make navigation work efficiently.

When to use it

  • ✓When starting a new SwiftUI app.
  • ✓When trying to organize existing app code.
  • ✓When the app is slow or not responsive.

Example of what to ask your AI

“How can I better organize my SwiftUI code for data management and navigation?”

Want more? Open the sections below.

How to use

The easiest way: copy the message below and paste it into Claude, ChatGPT, Cursor or any AI. It reads the skill and follows the instructions.

Skill link

https://github.com/affaan-m/ECC/blob/main/pi/core/skills/swiftui-patterns/SKILL.md

Ready-to-paste message

Use this skill to help me: https://github.com/affaan-m/ECC/blob/main/pi/core/skills/swiftui-patterns/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
How it works

It shows how to use specific SwiftUI tools like `@Observable` for data management and `NavigationStack` for navigation. The goal is to make the code cleaner and the app faster.

Installation

Easiest way: copy the skill link and paste it into Claude, ChatGPT or Cursor asking it to follow the instructions.

In the terminal (Claude Code, Codex, Cursor), in your project folder:

npx skills add affaan-m/ECC --skill swiftui-patterns

Or download just the file:

curl -L https://raw.githubusercontent.com/affaan-m/ECC/main/pi/core/skills/swiftui-patterns/SKILL.md -o SKILL.md
Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

SwiftUI Patterns

Modern SwiftUI patterns for building declarative, performant user interfaces on Apple platforms. Covers the Observation framework, view composition, type-safe navigation, and performance optimization.

When to Activate

  • Building SwiftUI views and managing state (@State, @Observable, @Binding)
  • Designing navigation flows with NavigationStack
  • Structuring view models and data flow
  • Optimizing rendering performance for lists and complex layouts
  • Working with environment values and dependency injection in SwiftUI

State Management

Property Wrapper Selection

Choose the simplest wrapper that fits:

Wrapper Use Case
@State View-local value types (toggles, form fields, sheet presentation)
@Binding Two-way reference to parent's @State
@Observable class + @State Owned model with multiple properties
@Observable class (no wrapper) Read-only reference passed from parent
@Bindable Two-way binding to an @Observable property
@Environment Shared dependencies injected via .environment()

@Observable ViewModel

Use @Observable (not ObservableObject) — it tracks property-level changes so SwiftUI only re-renders views that read the changed property:

@Observable
final class ItemListViewModel {
    private(set) var items: [Item] = []
    private(set) var isLoading = false
    var searchText = ""

    private let repository: any ItemRepository

    init(repository: any ItemRepository = DefaultItemRepository()) {
        self.repository = repository
    }

    func load() async {
        isLoading = true
        defer { isLoading = false }
        items = (try? await repository.fetchAll()) ?? []
    }
}

View Consuming the ViewModel

struct ItemListView: View {
    @State private var viewModel: ItemListViewModel

    init(viewModel: ItemListViewModel = ItemListViewModel()) {
        _viewModel = State(initialValue: viewModel)
    }

    var body: some View {
        List(viewModel.items) { item in
            ItemRow(item: item)
        }
        .searchable(text: $viewModel.searchText)
        .overlay { if viewModel.isLoading { ProgressView() } }
        .task { await viewModel.load() }
    }
}

Environment Injection

Replace @EnvironmentObject with @Environment:

// Inject
ContentView()
    .environment(authManager)

// Consume
struct ProfileView: View {
    @Environment(AuthManager.self) private var auth

    var body: some View {
        Text(auth.currentUser?.name ?? "Guest")
    }
}

View Composition

Extract Subviews to Limit Invalidation

Break views into small, focused structs. When state changes, only the subview reading that state re-renders:

struct OrderView: View {
    @State private var viewModel = OrderViewModel()

    var body: some View {
        VStack {
            OrderHeader(title: viewModel.title)
            OrderItemList(items: viewModel.items)
            OrderTotal(total: viewModel.total)
        }
    }
}

ViewModifier for Reusable Styling

struct CardModifier: ViewModifier {
    func body(content: Content) -> some View {
        content
            .padding()
            .background(.regularMaterial)
            .clipShape(RoundedRectangle(cornerRadius: 12))
    }
}

extension View {
    func cardStyle() -> some View {
        modifier(CardModifier())
    }
}

Navigation

Type-Safe NavigationStack

Use NavigationStack with NavigationPath for programmatic, type-safe routing:

@Observable
final class Router {
    var path = NavigationPath()

    func navigate(to destination: Destination) {
        path.append(destination)
    }

    func popToRoot() {
        path = NavigationPath()
    }
}

enum Destination: Hashable {
    case detail(Item.ID)
    case settings
    case profile(User.ID)
}

struct RootView: View {
    @State private var router = Router()

    var body: some View {
        NavigationStack(path: $router.path) {
            HomeView()
                .navigationDestination(for: Destination.self) { dest in
                    switch dest {
                    case .detail(let id): ItemDetailView(itemID: id)
                    case .settings: SettingsView()
                    case .profile(let id): ProfileView(userID: id)
                    }
                }
        }
        .environment(router)
    }
}

Performance

…

FAQ

What's the difference between `@State` and `@Observable`?

`@State` is for simple data belonging to a single screen. `@Observable` is for more complex data that can be shared and managed more efficiently, updating only the parts of the screen that actually need to be redrawn.

How do I make screen navigation more robust?

Use `NavigationStack` with `NavigationPath`. This allows you to clearly define where you can go and with what data, preventing common navigation errors.

Why is my app running slowly?

It might be that unnecessary parts of the screen are being redrawn. Use `LazyVStack` for large lists and break down your screens into smaller components so only what's needed gets updated.

Details

Original description by the author

SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices. Use when building or reviewing SwiftUI views, @Observable state, navigation, or render performance.

repo: affaan-m/ECC

Popularity

GitHub stars
280k
Ranking position
#127
License
MIT
Updated
10/15/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills