swift-patterns
Review, refactor, or build SwiftUI features with correct state management, modern API usage, optimal view composition, navigation patterns, performance optimization, and testing best practices.
git clone https://github.com/efremidze/swift-patterns-skill
T=$(mktemp -d) && git clone --depth=1 https://github.com/efremidze/swift-patterns-skill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/swift-patterns" ~/.claude/skills/efremidze-swift-patterns-skill-swift-patterns && rm -rf "$T"
swift-patterns/SKILL.mdswift-patterns
Review, refactor, or build SwiftUI features with correct state management, modern API usage, optimal view composition, and performance-conscious patterns. Prioritize native APIs, Apple design guidance, and testable code structure. This skill focuses on facts and best practices without enforcing specific architectural patterns.
Workflow Decision Tree
1) Review existing SwiftUI code
→ Read
references/workflows-review.md for review methodology
- Check state management against property wrapper selection (see
)references/state.md - Verify view composition and extraction patterns (see
)references/view-composition.md - Audit list performance and identity stability (see
)references/lists-collections.md - Validate modern API usage (see
)references/modern-swiftui-apis.md - Check async work patterns with .task (see
)references/concurrency.md - Verify navigation implementation (see
)references/navigation.md - Use Review Response Template (below)
2) Refactor existing SwiftUI code
→ Read
references/workflows-refactor.md for refactor methodology
- Extract complex views using playbooks (see
)references/refactor-playbooks.md - Migrate deprecated APIs to modern equivalents (see
)references/modern-swiftui-apis.md - Optimize performance hot paths (see
)references/performance.md - Restructure state ownership (see
)references/state.md - Apply common patterns (see
)references/patterns.md - Use Refactor Response Template (below)
3) Implement new SwiftUI features
- Design data flow first: identify owned vs injected state (see
)references/state.md - Structure views for optimal composition (see
)references/view-composition.md - Use modern APIs only (see
)references/modern-swiftui-apis.md - Handle async work with .task modifier (see
)references/concurrency.md - Apply performance patterns early (see
)references/performance.md - Implement navigation flows (see
)references/navigation.md
4) Answer best practice questions
- Load relevant reference file(s) based on topic (see Reference Files below)
- Provide direct guidance with examples
If intent unclear, ask: "Do you want findings only (review), or should I change the code (refactor)?"
Response Templates
Review Response:
- Scope - What was reviewed
- Findings - Grouped by severity with actionable statements
- Evidence - File paths or code locations
- Risks and tradeoffs - What could break or needs attention
- Next steps - What to fix first or verify
Refactor Response:
- Intent + scope - What is being changed and why
- Changes - Bulleted list with file paths
- Behavior preservation - What should remain unchanged
- Next steps - Tests or verification needed
Quick Reference: Property Wrapper Selection
| Wrapper | Use When | Ownership |
|---|---|---|
| Internal view state (value type or class) | View owns |
| Child needs to modify parent's state | Parent owns |
| Injected object needing bindings | Injected |
| Read-only value from parent | Injected |
+ | Read-only value needing reactive updates | Injected |
Key rules:
- Always mark
as@State
(makes ownership explicit)private - Never use
for passed values (accepts initial value only)@State - Use
with@State
classes (not@Observable
)@StateObject
See
references/state.md for detailed guidance and tradeoffs.
Quick Reference: Modern API Replacements
| Deprecated | Modern Alternative | Notes |
|---|---|---|
| | Supports dynamic type |
| | More flexible |
| | Type-safe navigation |
| API | iOS 18+ |
| | Unless need location/count |
| or | Two or zero parameters |
| or layout APIs | Avoid hard-coded sizes |
See
references/modern-swiftui-apis.md for complete migration guide.
Review Checklist
Use this when reviewing SwiftUI code:
State Management
-
properties marked@Stateprivate - Passed values NOT declared as
or@State@StateObject -
only where child modifies parent state@Binding - Property wrapper selection follows ownership rules
- State ownership clear and intentional
Modern APIs
- No deprecated modifiers (foregroundColor, cornerRadius, etc.)
- Using
instead ofNavigationStackNavigationView - Using
instead ofButton
when appropriateonTapGesture - Using two-parameter or no-parameter
onChange()
View Composition
- Using modifiers over conditionals for state changes (maintains identity)
- Complex views extracted to separate subviews
- Views kept small and focused
- View
simple and pure (no side effects)body
Navigation & Sheets
- Using
for type-safe navigationnavigationDestination(for:) - Using
for model-based sheets.sheet(item:) - Sheets own their dismiss actions
Lists & Collections
-
uses stable identity (neverForEach
for dynamic data).indices - Constant number of views per
elementForEach - No inline filtering in
(prefilter and cache)ForEach - No
in list rowsAnyView
Performance
- Passing only needed values to views (not large config objects)
- Eliminating unnecessary dependencies
- Checking value changes before state assignment in hot paths
- Using
/LazyVStack
for large listsLazyHStack - No object creation in view
body
Async Work
- Using
for automatic cancellation.task - Using
for value-dependent tasks.task(id:) - Not mixing
with async work.onAppear
See reference files for detailed explanations of each item.
Constraints
- Swift/SwiftUI focus only - Exclude server-side Swift and UIKit unless bridging required
- No Swift concurrency patterns - Use
for SwiftUI async work.task - No architecture mandates - Don't require MVVM/MVC/VIPER or specific structures
- No formatting/linting rules - Focus on correctness and patterns
- No tool-specific guidance - No Xcode, Instruments, or IDE instructions
- Citations allowed:
,developer.apple.com/documentation/swiftui/developer.apple.com/documentation/swift/
All workflows must follow these constraints.
Philosophy
This skill focuses on facts and best practices from Apple's documentation:
- Modern APIs over deprecated ones
- Clear state ownership patterns
- Performance-conscious view composition
- Testable code structure
- No architectural mandates (MVVM/VIPER not required)
- Apple Human Interface Guidelines adherence
Reference Files
All references in
references/:
- Review methodology and findings taxonomyworkflows-review.md
- Refactor methodology and invariantsworkflows-refactor.md
- Step-by-step refactor guidesrefactor-playbooks.md
- Property wrappers and ownership patternsstate.md
- Navigation implementation patternsnavigation.md
- View structure and extractionview-composition.md
- Identity and ForEach patternslists-collections.md
- Scroll handling and paginationscrolling.md
- Async work with .taskconcurrency.md
- Optimization strategiesperformance.md
- Testing and dependency injectiontesting-di.md
- Common SwiftUI patternspatterns.md
- Legacy API migrationmodern-swiftui-apis.md
- Code quality checkscode-review-refactoring.md