AgentSkillsCN

swift-swiftui

提供SwiftUI的Model-View架构模式,包括基于枚举的状态管理、模型组合、依赖注入、视图标识,以及可观察模型的约定规范。当您需要构建SwiftUI视图、创建可观察模型、实施状态管理,或将用例与UI进行高效衔接时,可使用此技能。

SKILL.md
--- frontmatter
name: swift-swiftui
description: Provides SwiftUI Model-View architecture patterns including enum-based state, model composition, dependency injection, view identity, and observable model conventions. Use when building SwiftUI views, creating observable models, implementing state management, or connecting use cases to the UI.
user-invocable: true

SwiftUI Patterns

SwiftUI follows Model-View (MV) — not MVVM. Views connect directly to @Observable models that live only in the Apps layer. Models monitor use case streams and update state for the UI. Business logic belongs in Features (use cases) and SDKs (clients), not models.

Key rules:

  • No dedicated ViewModels per view — models span many views
  • @MainActor on all @Observable models
  • Store root models in the App struct to avoid re-initialization on view rebuilds

Detailed Guides

TopicWhen to UseDocument
Enum-based state, state flow, state ownershipDefining model state or consuming use case streamsmodel-state.md
Parent/child models, optional models, lifecycleComposing models or managing model initializationmodel-composition.md
Environment injection, view-scoped modelsInjecting models into viewsdependency-injection.md
.id() modifier for view resetResetting view state when dependencies changeview-identity.md
Domain structs, prerequisite dataCreating data types or handling missing data in viewsdata-models.md

Source Documentation

  • swift-ui.md — Full SwiftUI architecture guidelines