A comprehensive guide to state management and SSOT principles in SwiftUI.
Directory Structure
swiftui-ssot/
├── SKILL.md (このファイル)
└── references/
└── ssot.md
Reference Files
references/ssot.md
A comprehensive guide to Single Source of Truth (SSOT):
- The Three Pillars of SSOT: single source of truth, derived state, unidirectional data flow
- Property Wrapper Selection Flowchart: @State/@Binding/@StateObject/@ObservedObject/@EnvironmentObject
- Guide by Pattern:
- Eliminating Duplicate State
- Derived State (Computed Property)
- State Lifting
- Communication Between Sibling Views
- Appropriate Use of EnvironmentObject
- How to Detect Violations: code smells, search patterns
- Implementing SSO with iOS 17+ @Observable
- Review Checklist
How to Use
When designing state
references/ssot.mdrefer to the Property Wrapper Selection Flowchart- Clarify the owner of the data
- Do not hold derivable state
When state-related problems occur
references/ssot.mdcheck the violation patterns- Apply the solution for the relevant pattern
When reviewing a PR
references/ssot.mduse the review checklist
The Three Pillars of SSOT
- Single Source of Truth: each piece of data is managed in only one place
- Derived State: do not hold anything that can be computed from other state
- Unidirectional Data Flow: pass data from parent to child; use actions from child to parent
Property Wrapper Selection Guide
| Situation | Recommended |
|---|---|
| Local state within a View | @State |
| Value received from a parent (read/write) | @Binding |
| ObservableObject owned by the View | @StateObject |
| ObservableObject injected from outside | @ObservedObject |
| Shared across the entire app | @EnvironmentObject |
| iOS 17+ | @Observable + @Bindable |
Related Skills
- swiftui-coding-guidelines: basic best practices
- swift-ios-migration: iOS 17 @Observable migration