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
- •
@MainActoron all@Observablemodels - •Store root models in the
Appstruct to avoid re-initialization on view rebuilds
Detailed Guides
| Topic | When to Use | Document |
|---|---|---|
| Enum-based state, state flow, state ownership | Defining model state or consuming use case streams | model-state.md |
| Parent/child models, optional models, lifecycle | Composing models or managing model initialization | model-composition.md |
| Environment injection, view-scoped models | Injecting models into views | dependency-injection.md |
.id() modifier for view reset | Resetting view state when dependencies change | view-identity.md |
| Domain structs, prerequisite data | Creating data types or handling missing data in views | data-models.md |
Source Documentation
- •swift-ui.md — Full SwiftUI architecture guidelines