Get the FREE Ultimate OpenClaw Setup Guide →

swiftui-development

Scanned
npx machina-cli add skill ProjAnvil/MindForge/swiftui-development --openclaw
Files (1)
SKILL.md
3.2 KB

SwiftUI Development Skill

Instructions

Use this skill to write declarative, reactive UI code. Always adhere to the iOS Human Interface Guidelines (HIG).

What This Skill Does

  • UI Construction: Build interfaces using Views, Modifiers, and Layout containers.
  • State Management: Manage data flow using the Observation framework (@Observable, @State, @Environment).
  • Navigation: Handle routing using NavigationStack and navigationDestination.
  • Previews: Rapidly iterate on UI using the #Preview macro.
  • Persistence Integration: Seamlessly integrate SwiftData (@Query) into views.

When to Use This Skill

  • When writing .swift view files.
  • When designing the App's navigation structure.
  • When handling animations and transitions.
  • When binding ViewModel data to the interface.

Examples

Example 1: Observation-based ViewModel and View

@Observable
class CounterModel {
    var count = 0
    
    func increment() {
        count += 1
    }
}

struct CounterView: View {
    @State private var model = CounterModel()
    
    var body: some View {
        VStack {
            Text("Count: \(model.count)")
                .font(.largeTitle)
            Button("Add") {
                model.increment()
            }
        }
    }
}

#Preview {
    CounterView()
}

Example 2: Type-Safe Navigation with NavigationStack

enum Route: Hashable {
    case profile(id: String)
    case settings
}

struct ContentView: View {
    @State private var path = NavigationPath()
    
    var body: some View {
        NavigationStack(path: $path) {
            List {
                NavigationLink("Go to Profile", value: Route.profile(id: "123"))
                NavigationLink("Settings", value: Route.settings)
            }
            .navigationDestination(for: Route.self) { route in
                switch route {
                case .profile(let id):
                    ProfileView(userId: id)
                case .settings:
                    SettingsView()
                }
            }
        }
    }
}

Best Practices

  • Single Source of Truth: Always maintain a single source of truth for your data.
  • Environment: Use .environment() to inject global dependencies or theme data.
  • Avoid Massive Views: Break down complex views into smaller component views (struct SubView: View).
  • State vs Binding: Use @State when the view owns the data, and @Binding (@Bindable for Observables) when it modifies parent data.
  • Computed Properties: Use computed properties to derive UI state instead of manually syncing multiple state variables.

Notes

  • For iOS 17+, @Observable is preferred. For legacy support, fallback to ObservableObject may be needed, but strictly mark it.
  • All UI updates must be performed on the main thread (SwiftUI generally handles this, but be careful with callbacks from background tasks).

Best Practices

  • [Best practice 1]
  • [Best practice 2]
  • [Best practice 3]

Notes

[Any additional notes, warnings, or important considerations]

Source

git clone https://github.com/ProjAnvil/MindForge/blob/main/skills/en/swiftui-development/SKILL.mdView on GitHub

Overview

This skill focuses on building UIs with modern SwiftUI, including NavigationStack, the Observation framework, and SwiftData integration. It emphasizes declarative, reactive UI that follows iOS Human Interface Guidelines and supports fast iteration with previews.

How This Skill Works

You compose UI from Views, Modifiers, and layout containers, and drive data with the Observation framework (@Observable, @State, @Environment). Navigation is implemented with NavigationStack and navigationDestination using a type-safe Route enum, while SwiftData can be queried via @Query inside views; use #Preview for rapid iteration.

When to Use It

  • When writing .swift view files.
  • When designing the App's navigation structure.
  • When handling animations and transitions.
  • When binding ViewModel data to the interface.
  • When iterating UI quickly with the #Preview macro.

Quick Start

  1. Step 1: Create an @Observable model (e.g., CounterModel) and a SwiftUI view that binds to it.
  2. Step 2: Build a NavigationStack with a @State path and a navigationDestination mapping for routes.
  3. Step 3: Integrate SwiftData with @Query in the view and use #Preview to iterate quickly.

Best Practices

  • Single Source of Truth: keep data origins centralized.
  • Environment: inject global dependencies or theme data with .environment().
  • Avoid Massive Views: break into smaller SubView structs.
  • State vs Binding: use @State for owned data, @Binding/@Bindable for parent data.
  • Computed Properties: derive UI state with computed properties instead of syncing many state vars.

Example Use Cases

  • Example 1: Observation-based ViewModel and View (CounterModel with @Observable).
  • Example 2: Type-safe navigation with NavigationStack (Route enum and navigationDestination).
  • Example 3: SwiftData integration in a view using @Query to fetch persisted items.
  • Example 4: Profile/Settings flow using a NavigationStack and destination views.
  • Example 5: Rapid UI iteration using #Preview to test layouts and components.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers