Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Generate SwiftUI views that render FOSMVVM ViewModels. Scaffolds ViewModelView pattern with binding, loading states, and previews.
.claude/skills/fosmvvm-swiftui-view-generator/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-16 | ✗→✓ | ▲ Improved | — | — |
| case-08 | ✗→✓ | ▲ Improved | — | — |
| case-14 | ✗→✓ | ▲ Improved | — | — |
| case-19 | ✗→✓ | ▲ Improved | — | — |
| case-03 | ✗→✓ | ▲ Improved | — | — |
Generate SwiftUI views that render FOSMVVM ViewModels.
> For full architecture context, see FOSMVVMArchitecture.md | OpenClaw reference
In FOSMVVM, Views are thin rendering layers that display ViewModels:
┌─────────────────────────────────────────────────────────────┐
│ ViewModelView Pattern │
├─────────────────────────────────────────────────────────────┤
│ │
│ ViewModel (Data) ViewModelView (SwiftUI) │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ title: String │────►│ Text(vm.title) │ │
│ │ items: [Item] │────►│ ForEach(vm.items)│ │
│ │ isEnabled: Bool │────►│ .disabled(!...) │ │
│ └──────────────────┘ └──────────────────┘ │
│ │
│ Operations (Actions) │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ submit() │◄────│ Button(action:) │ │
│ │ cancel() │◄────│ .onAppear { } │ │
│ └──────────────────┘ └──────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘Key principle: Views don't transform or compute data. They render what the ViewModel provides.
The View filename should match the ViewModel it renders.
Sources/
{ViewModelsTarget}/
{Feature}/
{Feature}ViewModel.swift ←──┐
{Entity}CardViewModel.swift ←──┼── Same names
│
{ViewsTarget}/ │
{Feature}/ │
{Feature}View.swift ────┤ (renders {Feature}ViewModel)
{Entity}CardView.swift ────┘ (renders {Entity}CardViewModel)This alignment provides:
Every view conforms to ViewModelView:
swiftpublic struct MyView: ViewModelView { private let viewModel: MyViewModel public var body: some View { Text(viewModel.title) } public init(viewModel: MyViewModel) { self.viewModel = viewModel } }
Required:
private let viewModel: {ViewModel}public init(viewModel:)ViewModelView protocolInteractive views have operations:
swiftpublic struct MyView: ViewModelView { private let viewModel: MyViewModel private let operations: MyViewModelOperations #if DEBUG @State private var repaintToggle = false #endif public var body: some View { Button(action: performAction) { Text(viewModel.buttonLabel) } #if DEBUG .testDataTransporter(viewModelOps: operations, repaintToggle: $repaintToggle) #endif } public init(viewModel: MyViewModel) { self.viewModel = viewModel self.operations = viewModel.operations } private func performAction() { operations.performAction() toggleRepaint() } private func toggleRepaint() { #if DEBUG repaintToggle.toggle() #endif } }
When views have operations:
operations from viewModel.operations in init@State private var repaintToggle = false (DEBUG only).testDataTransporter(viewModelOps:repaintToggle:) modifier (DEBUG only)toggleRepaint() after every operation invocationParent views bind child views using .bind(appState:):
swiftpublic struct ParentView: ViewModelView { @Environment(AppState.self) private var appState private let viewModel: ParentViewModel public var body: some View { VStack { Text(viewModel.title) // Bind child view with subset of parent's data ChildView.bind( appState: .init( itemId: viewModel.selectedId, isConnected: viewModel.isConnected ) ) } } }
The .bind() pattern:
.bind(appState:) to receive data from parentAppState from its own ViewModel dataForms use FormFieldView and Validations environment:
swiftpublic struct MyFormView: ViewModelView { @Environment(Validations.self) private var validations @Environment(\.focusState) private var focusField @State private var error: Error? private let viewModel: MyFormViewModel private let operations: MyFormViewModelOperations public var body: some View { Form { FormFieldView( fieldModel: viewModel.$email, focusField: focusField, fieldValidator: viewModel.validateEmail, validations: validations ) Button(errorBinding: $error, asyncAction: submit) { Text(viewModel.submitButtonLabel) } .disabled(validations.hasError) } .onAsyncSubmit { await submit() } .alert( errorBinding: $error, title: viewModel.errorTitle, message: viewModel.errorMessage, dismissButtonLabel: viewModel.dismissButtonLabel ) } }
Form patterns:
@Environment(Validations.self) for validation stateFormFieldView for each input fieldButton(errorBinding:asyncAction:) for async actions.disabled(validations.hasError) on submit buttonUse .previewHost() for SwiftUI previews:
swift#if DEBUG #Preview { MyView.previewHost( bundle: MyAppResourceAccess.localizationBundle ) .environment(AppState()) } #Preview("With Data") { MyView.previewHost( bundle: MyAppResourceAccess.localizationBundle, viewModel: .stub(title: "Preview Title") ) .environment(AppState()) } #endif
Views that just render data (no user interactions):
swiftpublic struct InfoView: ViewModelView { private let viewModel: InfoViewModel public var body: some View { VStack { Text(viewModel.title) Text(viewModel.description) if viewModel.isActive { Text(viewModel.activeStatusLabel) } } } public init(viewModel: InfoViewModel) { self.viewModel = viewModel } }
Characteristics:
operations propertyrepaintToggle or testDataTransporterViews with user actions:
swiftpublic struct ActionView: ViewModelView { @State private var error: Error? private let viewModel: ActionViewModel private let operations: ActionViewModelOperations #if DEBUG @State private var repaintToggle = false #endif public var body: some View { VStack { Button(action: performAction) { Text(viewModel.actionLabel) } Button(role: .cancel, action: cancel) { Text(viewModel.cancelLabel) } } .alert( errorBinding: $error, title: viewModel.errorTitle, message: viewModel.errorMessage, dismissButtonLabel: viewModel.dismissButtonLabel ) #if DEBUG .testDataTransporter(viewModelOps: operations, repaintToggle: $repaintToggle) #endif } public init(viewModel: ActionViewModel) { self.viewModel = viewModel self.operations = viewModel.operations } private func performAction() { operations.performAction() toggleRepaint() } private func cancel() { operations.cancel() toggleRepaint() } private func toggleRepaint() { #if DEBUG repaintToggle.toggle() #endif } }
Views with validated input fields:
FormFieldView for each input@Environment(Validations.self) for validation statevalidations.hasErrorViews that compose child views:
swiftpublic struct ContainerView: ViewModelView { @Environment(AppState.self) private var appState private let viewModel: ContainerViewModel private let operations: ContainerViewModelOperations public var body: some View { VStack { switch viewModel.state { case .loading: ProgressView() case .ready: ChildAView.bind( appState: .init(id: viewModel.selectedId) ) ChildBView.bind( appState: .init( isActive: viewModel.isActive, level: viewModel.level ) ) } } } }
| File | Location | Purpose | |------|----------|---------| | {ViewName}View.swift | Sources/{ViewsTarget}/{Feature}/ | The SwiftUI view |
Note: The corresponding ViewModel and ViewModelOperations should already exist (use fosmvvm-viewmodel-generator skill).
| Placeholder | Description | Example | |-------------|-------------|---------| | {ViewName} | View name (without "View" suffix) | TaskList, SignIn | | {ViewsTarget} | SwiftUI views SPM target | MyAppViews | | {Feature} | Feature/module grouping | Tasks, Auth |
This skill references conversation context to determine view structure:
From conversation context, the skill identifies:
Based on view type:
.bind() callsGenerates view file with:
ViewModelView protocol conformanceSkill references information from:
swift@State private var error: Error? var body: some View { VStack { Button(errorBinding: $error, asyncAction: submit) { Text(viewModel.submitLabel) } } .alert( errorBinding: $error, title: viewModel.errorTitle, message: viewModel.errorMessage, dismissButtonLabel: viewModel.dismissButtonLabel ) } private func submit() async { do { try await operations.submit() } catch { self.error = error } toggleRepaint() }
For forms, handle validation errors separately:
swiftprivate func submit() async { let validations = validations do { try await operations.submit(data: viewModel.data) } catch let error as MyRequest.ResponseError { if !error.validationResults.isEmpty { validations.replace(with: error.validationResults) } else { self.error = error } } catch { self.error = error } toggleRepaint() }
swiftvar body: some View { VStack { if isLoading { ProgressView() } else { contentView } } .task(errorBinding: $error) { try await loadData() } } private func loadData() async throws { isLoading = true try await operations.loadData() isLoading = false toggleRepaint() }
Use ViewModel state for conditionals:
swiftvar body: some View { VStack { if viewModel.isEmpty { Text(viewModel.emptyStateMessage) } else { ForEach(viewModel.items) { item in ItemRow(item: item) } } } }
Extract reusable view fragments as computed properties:
swiftprivate var headerView: some View { HStack { Text(viewModel.title) Spacer() Image(systemName: viewModel.iconName) } } var body: some View { VStack { headerView contentView } }
When a view needs to render multiple possible ViewModels (success, various error types), use an enum wrapper:
The Wrapper ViewModel:
swift@ViewModel public struct TaskResultViewModel { public enum Result { case success(TaskViewModel) case notFound(NotFoundViewModel) case validationError(ValidationErrorViewModel) case permissionDenied(PermissionDeniedViewModel) } public let result: Result public var vmId: ViewModelId = .init(type: Self.self) public init(result: Result) { self.result = result } }
The View:
swiftpublic struct TaskResultView: ViewModelView { private let viewModel: TaskResultViewModel public var body: some View { switch viewModel.result { case .success(let vm): TaskView(viewModel: vm) case .notFound(let vm): NotFoundView(viewModel: vm) case .validationError(let vm): ValidationErrorView(viewModel: vm) case .permissionDenied(let vm): PermissionDeniedView(viewModel: vm) } } public init(viewModel: TaskResultViewModel) { self.viewModel = viewModel } }
Key principles:
any ViewModel existentials)IMPORTANT: ViewModelId controls SwiftUI's view identity system via the .id(vmId) modifier. Incorrect initialization causes SwiftUI to treat different data as the same view, breaking updates.
❌ WRONG - Never use this:
swiftpublic var vmId: ViewModelId = .init() // NO! Generic identity
✅ MINIMUM - Use type-based identity:
swiftpublic var vmId: ViewModelId = .init(type: Self.self)
This ensures views of the same type get unique identities.
✅ IDEAL - Use data-based identity when available:
swiftpublic struct TaskViewModel { public let id: ModelIdType public var vmId: ViewModelId public init(id: ModelIdType, /* other params */) { self.id = id self.vmId = .init(id: id) // Ties view identity to data identity // ... } }
Why this matters:
.id() modifier to determine when to recreate vs update viewsvmId provides this identity for ViewModelViews.init(id:)) is best because it ties view lifecycle to data lifecycleSources/{ViewsTarget}/
├── {Feature}/
│ ├── {Feature}View.swift # Full page → {Feature}ViewModel
│ ├── {Entity}CardView.swift # Child component → {Entity}CardViewModel
│ ├── {Entity}RowView.swift # Child component → {Entity}RowViewModel
│ └── {Modal}View.swift # Modal → {Modal}ViewModel
├── Shared/
│ ├── HeaderView.swift # Shared components
│ └── FooterView.swift
└── Styles/
└── ButtonStyles.swift # Reusable button stylesswift// ❌ BAD - View is transforming data var body: some View { Text("\(viewModel.firstName) \(viewModel.lastName)") } // ✅ GOOD - ViewModel provides shaped result var body: some View { Text(viewModel.fullName) // via @LocalizedCompoundString }
swift// ❌ BAD - Test infrastructure won't work private func submit() { operations.submit() // Missing toggleRepaint()! } // ✅ GOOD - Always call after operations private func submit() { operations.submit() toggleRepaint() }
swift// ❌ BAD - View is computing var body: some View { if !viewModel.items.isEmpty { Text("You have \(viewModel.items.count) items") } } // ✅ GOOD - ViewModel provides the state var body: some View { if viewModel.hasItems { Text(viewModel.itemCountMessage) } }
swift// ❌ BAD - Not localizable Button(action: submit) { Text("Submit") } // ✅ GOOD - ViewModel provides localized text Button(action: submit) { Text(viewModel.submitButtonLabel) }
swift// ❌ BAD - Errors not handled Button(action: submit) { Text(viewModel.submitLabel) } // ✅ GOOD - Error binding for async actions Button(errorBinding: $error, asyncAction: submit) { Text(viewModel.submitLabel) }
swift// ❌ BAD - Recomputed on every render public var body: some View { let operations = viewModel.operations Button(action: { operations.submit() }) { Text(viewModel.submitLabel) } } // ✅ GOOD - Store in init private let operations: MyOperations public init(viewModel: MyViewModel) { self.viewModel = viewModel self.operations = viewModel.operations }
// ❌ BAD - Filename doesn't match ViewModel
ViewModel: TaskListViewModel
View: TasksView.swift
// ✅ GOOD - Aligned names
ViewModel: TaskListViewModel
View: TaskListView.swiftswift// ❌ BAD - Generic identity, views won't update correctly public var vmId: ViewModelId = .init() // ✅ MINIMUM - Type-based identity public var vmId: ViewModelId = .init(type: Self.self) // ✅ IDEAL - Data-based identity (when id available) public init(id: ModelIdType) { self.id = id self.vmId = .init(id: id) }
swift// ❌ BAD - Force-unwrapping to work around missing overload import SwiftUI Text(try! viewModel.title.localizedString) // Anti-pattern - don't do this! Label(try! viewModel.label.localizedString, systemImage: "star") // ✅ GOOD - Request the proper SwiftUI overload instead // The correct solution is to add an init extension like this: extension Text { public init(_ localizable: Localizable) { self.init(localizable.localized) } } extension Label where Title == Text, Icon == Image { public init(_ title: Localizable, systemImage: String) { self.init(title.localized, systemImage: systemImage) } } // Then views use it cleanly without force-unwraps: Text(viewModel.title) Label(viewModel.label, systemImage: "star")
Why this matters:
FOSMVVM provides the Localizable protocol for all localized strings and includes SwiftUI init overloads for common elements like Text. However, not every SwiftUI element has a Localizable overload yet.
When you encounter a SwiftUI element that doesn't accept Localizable directly:
try! localizable.localizedString - this bypasses the type system and spreads force-unwrap calls throughout the view codeLocalizable and pass .localized to the standard initializerThis approach keeps the codebase clean, type-safe, and eliminates force-unwraps from view code entirely.
See reference.md for complete file templates.
| Concept | Convention | Example | |---------|------------|---------| | View struct | {Name}View | TaskListView, SignInView | | ViewModel property | viewModel | Always viewModel | | Operations property | operations | Always operations | | Error state | error | Always error | | Repaint toggle | repaintToggle | Always repaintToggle |
swift// Error alert with ViewModel strings .alert( errorBinding: $error, title: viewModel.errorTitle, message: viewModel.errorMessage, dismissButtonLabel: viewModel.dismissButtonLabel ) // Async task with error handling .task(errorBinding: $error) { try await loadData() } // Async submit handler .onAsyncSubmit { await submit() } // Test data transporter (DEBUG only) .testDataTransporter(viewModelOps: operations, repaintToggle: $repaintToggle) // UI testing identifier .uiTestingIdentifier("submitButton")
Apply standard modifiers as needed for layout, styling, etc.
Invocation:
bash/fosmvvm-swiftui-view-generator
Prerequisites:
Output:
{ViewName}View.swift - SwiftUI view conforming to ViewModelView protocolWorkflow integration: This skill is typically used after discussing requirements or reading specification files. The skill references that context automatically—no file paths or Q&A needed.
| Version | Date | Changes | |---------|------|---------| | 1.0 | 2026-01-23 | Initial skill for SwiftUI view generation |
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-13 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-10 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-16 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-06 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-08 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-14 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-19 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-03 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-07 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-09 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-12 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-05 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-15 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-17 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-02 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-22 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-11 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-20 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-04 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-01 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-18 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-21 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +68 percentage points is the difference between those two pass rates over the 22 comparable cases.
The per-case answers from this run were removed by the retention sweep, so the case table below shows the verdicts without the text either arm produced. The counts above were recorded at the time and are unaffected. Answers are now kept for 180 days.
Other measured skills in the registry, with their headline benchmark lift.