Skip to content

skillMobileswift

SwiftUI Patterns

Structures SwiftUI views, state and navigation around observable models so screens stay small, previewable and free of stale-state bugs.

npx workspacetemplates add swiftui-patterns --tool claude-code

Writes 1 file to .claude/skills/swiftui-patterns/

.claude/skills/swiftui-patterns/SKILL.md29 lines · 1.8 KBFile
---
name: swiftui-patterns
description: Structures SwiftUI views, state and navigation around observable models so screens stay small, previewable and free of stale-state bugs.
---

# SwiftUI patterns

Small views, one source of truth, navigation as data.

## Steps

1. Check the deployment target. On iOS 17 and later use `@Observable` classes. On older targets keep `ObservableObject` and `@Published`, and say so.
2. Split each screen into a model and a view. The model is an `@Observable` `@MainActor` class holding state and async methods. The view reads it and sends actions. No networking or formatting logic in `body`.
3. Ownership: the view that creates a model holds it with `@State`. Children receive it as a plain property, or `@Bindable` when they need bindings. Use `@Environment` for app-wide services only. Never create a model inside `body`.
4. Represent screen state as an enum such as `idle`, `loading`, `loaded(Items)`, `failed(Error)`, and switch over it in the view.
5. Navigation: use `NavigationStack` with a `path` array of `Hashable` route values and `navigationDestination(for:)`. Keep the path in one place so deep links and "pop to root" are one assignment.
6. Lists: give `ForEach` stable `Identifiable` ids from the data, never `\.self` on mutable values or array indices.
7. Async work goes in `.task` or `.task(id:)`, which cancels itself. Never start `Task {}` from `onAppear` without cancellation.
8. Extract subviews when a section has its own state or exceeds about 40 lines. Add a `#Preview` for each state of the enum.

## Rules

- No force unwraps in views.
- Do not mark whole views `@MainActor` to silence warnings. Mark the model.
- Build with strict concurrency warnings on and fix them.

## Report

Files created or split, the route type, and any ownership bugs found.

What it does

SwiftUI code drifts into giant views with a dozen property wrappers and navigation that breaks on the second tap. This skill gives the agent a fixed way to split a screen into a model, a thin view and a navigation path, using the modern Observation framework.

It also covers the quiet mistakes: state owned by the wrong view, identity that changes on every refresh, and work started in the body instead of a task.

Example

you › Refactor ProfileView. It is 520 lines and the avatar flickers when I refresh.

agent › Split into ProfileModel (@Observable, @MainActor), ProfileView (92 lines) and AvatarHeader, StatsRow and SettingsList subviews. Flicker cause: ForEach used \.self on structs that change on refresh, so every row got a new identity. Switched to id: \.id. Added previews for loading, loaded and failed. Build clean with strict concurrency.

Questions

Does it require iOS 17?
No. It checks your deployment target and falls back to ObservableObject patterns when Observation is not available, keeping the same split of model and view.
Why a path array for navigation?
Navigation held as plain data can be saved, restored, tested and driven from a link. Scattered NavigationLink destinations cannot.
  • Flutter State Management

    Skillflutter

    Free
  • Kotlin Android Compose

    Skillkotlin

    Free
  • Mobile App Release

    SkillMobile

    Free
  • Offline First Sync

    SkillMobile

    Free