All skills
dpearson2699 avatar

/swiftui-patterns

@cf3fe87

Builds and reviews SwiftUI views with modern MV architecture, state, composition, isolated previews, and migration guidance. Covers @Observable ownership, @State/@Bindable/@Environment wiring, view decomposition, ViewModifiers, environment values, .task loading, iOS 26+ handoffs, Writing Tools, clipboard availability, and performance. Use when structuring SwiftUI state, managing @Observable, composing views, previewing meaningful UI states, or correcting SwiftUI patterns.

Use this Skill: https://skilld.dev/gh/dpearson2699/swift-ios-skills/swiftui-patterns

This session only. Nothing lands on disk.

referencespreview-isolation.md

≈667 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Isolated Preview Construction

Use previews as deterministic compositions of a view's required inputs. A preview should render without launching the full app or reaching production services.

Cover Meaningful States

Provide separate previews for the states that materially change the interface:

  • loaded content with representative data
  • meaningful loading or refreshing UI
  • empty content and its primary recovery action
  • recoverable and terminal error presentations when they differ

Skip state permutations that do not change layout, behavior, or accessibility.

Install Required Dependencies

Keep production dependencies required. Install every environment value, observable model, action, and persistence container the view expects instead of making a dependency optional to silence a preview failure.

@MainActor
private struct ProfilePreview: View {
    let state: ProfileStore.State

    var body: some View {
        ProfileScreen()
            .environment(ProfileStore.preview(state: state))
            .environment(\.profileClient, .preview)
    }
}

#Preview("Loaded") {
    ProfilePreview(state: .loaded(.fixture))
}

#Preview("Loading") {
    ProfilePreview(state: .loading)
}

#Preview("Empty") {
    ProfilePreview(state: .empty)
}

#Preview("Error") {
    ProfilePreview(state: .failed(.fixture))
}

When a view requires SwiftData, use a seeded in-memory ModelContainer. Apply the same rule to caches and file stores: create isolated temporary or in-memory fixtures.

Keep Fixtures Deterministic

  • Use fixed identifiers, dates, locale-sensitive values, and image dimensions.
  • Make preview clients return fixture results immediately or after a controlled delay.
  • Keep fixtures small but representative enough to exercise wrapping, truncation, and empty/error affordances.
  • Put reusable preview factories next to the model or in preview-only support code; do not hide them behind production singletons.

Never depend on live networking, authentication state, production databases, keychain contents, or global singleton state. These inputs make previews slow, flaky, order-dependent, or destructive.

Review Checklist

  • Loaded plus meaningful loading, empty, and error states are represented
  • Fixtures are fixed and repeatable
  • Every required environment dependency is installed
  • Persistence is in-memory or temporary and seeded locally
  • No live service, authentication, production data, keychain, or global singleton is required
  • Required production dependencies remain nonoptional

Apple session: Visually edit SwiftUI views

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill is a comprehensive collection of SwiftUI development patterns for iOS, covering modern architecture, state management, and UI composition. It provides best practices for Swift 6 and modern platform integrations without any detected security risks.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    1/5 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at cf3fe87. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Steadyupdated 3 months ago
  • Performance
  • swiftui
  • ios
  • state-management
  • architecture
  • view-composition
  • observable
  • async-loading
  • environment
  • swift

README badge

README badge for dpearson2699/swift-ios-skills/swiftui-patterns

Guides SwiftUI view architecture with the Model-View pattern, @Observable state management, view composition, environment wiring, and async data loading for iOS 17+. Use when structuring a SwiftUI app, managing @Observable ownership, decomposing views, or applying state synchronization patterns like .task and @Bindable.

Generated from the current SKILL.md.

Does this skill cover navigation patterns like NavigationStack and tabs?
No. Navigation patterns are covered in the dedicated swiftui-navigation skill. This skill focuses on state management, view composition, and environment wiring.
What iOS versions does this skill target?
iOS 26+ with Swift 6.3, with most patterns backward-compatible to iOS 17 unless explicitly noted (e.g., Writing Tools require iOS 18+).
Does this cover layout and component patterns like grids and lists?
No. Detailed layout, container, and component patterns are covered in the swiftui-layout-components skill.
Should I use @Observable with @MainActor?
Yes. Always annotate @Observable view model classes with @MainActor to ensure UI-bound state updates on the main thread and comply with Swift 6 concurrency safety.
When should I use a view model versus the MV pattern?
Default to the MV pattern where views are lightweight state expressions and models own business logic. Only introduce view models if the existing codebase already uses them.

Generated from the current SKILL.md. These answers refresh after source changes.