Review, refactor, or build SwiftUI features with correct state management, modern API usage, optimal view composition, navigation patterns, performance optimization, and testing best practices.
Add this skill
npx mdskills install efremidze/swift-patternsComprehensive SwiftUI review and refactoring framework with clear workflows, decision trees, and extensive reference materials
1---2name: swift-patterns3description: Review, refactor, or build SwiftUI features with correct state management, modern API usage, optimal view composition, navigation patterns, performance optimization, and testing best practices.4---56# swift-patterns78Review, 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.910## Workflow Decision Tree1112### 1) Review existing SwiftUI code13→ Read `references/workflows-review.md` for review methodology14- Check state management against property wrapper selection (see `references/state.md`)15- Verify view composition and extraction patterns (see `references/view-composition.md`)16- Audit list performance and identity stability (see `references/lists-collections.md`)17- Validate modern API usage (see `references/modern-swiftui-apis.md`)18- Check async work patterns with .task (see `references/concurrency.md`)19- Verify navigation implementation (see `references/navigation.md`)20- Use Review Response Template (below)2122### 2) Refactor existing SwiftUI code23→ Read `references/workflows-refactor.md` for refactor methodology24- Extract complex views using playbooks (see `references/refactor-playbooks.md`)25- Migrate deprecated APIs to modern equivalents (see `references/modern-swiftui-apis.md`)26- Optimize performance hot paths (see `references/performance.md`)27- Restructure state ownership (see `references/state.md`)28- Apply common patterns (see `references/patterns.md`)29- Use Refactor Response Template (below)3031### 3) Implement new SwiftUI features32- Design data flow first: identify owned vs injected state (see `references/state.md`)33- Structure views for optimal composition (see `references/view-composition.md`)34- Use modern APIs only (see `references/modern-swiftui-apis.md`)35- Handle async work with .task modifier (see `references/concurrency.md`)36- Apply performance patterns early (see `references/performance.md`)37- Implement navigation flows (see `references/navigation.md`)3839### 4) Answer best practice questions40- Load relevant reference file(s) based on topic (see Reference Files below)41- Provide direct guidance with examples4243**If intent unclear, ask:** "Do you want findings only (review), or should I change the code (refactor)?"4445## Response Templates4647**Review Response:**481. **Scope** - What was reviewed492. **Findings** - Grouped by severity with actionable statements503. **Evidence** - File paths or code locations514. **Risks and tradeoffs** - What could break or needs attention525. **Next steps** - What to fix first or verify5354**Refactor Response:**551. **Intent + scope** - What is being changed and why562. **Changes** - Bulleted list with file paths573. **Behavior preservation** - What should remain unchanged584. **Next steps** - Tests or verification needed5960## Quick Reference: Property Wrapper Selection6162| Wrapper | Use When | Ownership |63|---------|----------|-----------|64| `@State` | Internal view state (value type or `@Observable` class) | View owns |65| `@Binding` | Child needs to modify parent's state | Parent owns |66| `@Bindable` | Injected `@Observable` object needing bindings | Injected |67| `let` | Read-only value from parent | Injected |68| `var` + `.onChange()` | Read-only value needing reactive updates | Injected |6970**Key rules:**71- Always mark `@State` as `private` (makes ownership explicit)72- Never use `@State` for passed values (accepts initial value only)73- Use `@State` with `@Observable` classes (not `@StateObject`)7475See `references/state.md` for detailed guidance and tradeoffs.7677## Quick Reference: Modern API Replacements7879| Deprecated | Modern Alternative | Notes |80|------------|-------------------|-------|81| `foregroundColor()` | `foregroundStyle()` | Supports dynamic type |82| `cornerRadius()` | `.clipShape(.rect(cornerRadius:))` | More flexible |83| `NavigationView` | `NavigationStack` | Type-safe navigation |84| `tabItem()` | `Tab` API | iOS 18+ |85| `onTapGesture()` | `Button` | Unless need location/count |86| `onChange(of:) { value in }` | `onChange(of:) { old, new in }` or `onChange(of:) { }` | Two or zero parameters |87| `UIScreen.main.bounds` | `GeometryReader` or layout APIs | Avoid hard-coded sizes |8889See `references/modern-swiftui-apis.md` for complete migration guide.9091## Review Checklist9293Use this when reviewing SwiftUI code:9495### State Management96- [ ] `@State` properties marked `private`97- [ ] Passed values NOT declared as `@State` or `@StateObject`98- [ ] `@Binding` only where child modifies parent state99- [ ] Property wrapper selection follows ownership rules100- [ ] State ownership clear and intentional101102### Modern APIs103- [ ] No deprecated modifiers (foregroundColor, cornerRadius, etc.)104- [ ] Using `NavigationStack` instead of `NavigationView`105- [ ] Using `Button` instead of `onTapGesture` when appropriate106- [ ] Using two-parameter or no-parameter `onChange()`107108### View Composition109- [ ] Using modifiers over conditionals for state changes (maintains identity)110- [ ] Complex views extracted to separate subviews111- [ ] Views kept small and focused112- [ ] View `body` simple and pure (no side effects)113114### Navigation & Sheets115- [ ] Using `navigationDestination(for:)` for type-safe navigation116- [ ] Using `.sheet(item:)` for model-based sheets117- [ ] Sheets own their dismiss actions118119### Lists & Collections120- [ ] `ForEach` uses stable identity (never `.indices` for dynamic data)121- [ ] Constant number of views per `ForEach` element122- [ ] No inline filtering in `ForEach` (prefilter and cache)123- [ ] No `AnyView` in list rows124125### Performance126- [ ] Passing only needed values to views (not large config objects)127- [ ] Eliminating unnecessary dependencies128- [ ] Checking value changes before state assignment in hot paths129- [ ] Using `LazyVStack`/`LazyHStack` for large lists130- [ ] No object creation in view `body`131132### Async Work133- [ ] Using `.task` for automatic cancellation134- [ ] Using `.task(id:)` for value-dependent tasks135- [ ] Not mixing `.onAppear` with async work136137See reference files for detailed explanations of each item.138139## Constraints140141- **Swift/SwiftUI focus only** - Exclude server-side Swift and UIKit unless bridging required142- **No Swift concurrency patterns** - Use `.task` for SwiftUI async work143- **No architecture mandates** - Don't require MVVM/MVC/VIPER or specific structures144- **No formatting/linting rules** - Focus on correctness and patterns145- **No tool-specific guidance** - No Xcode, Instruments, or IDE instructions146- **Citations allowed:** `developer.apple.com/documentation/swiftui/`, `developer.apple.com/documentation/swift/`147148All workflows must follow these constraints.149150## Philosophy151152This skill focuses on **facts and best practices** from Apple's documentation:153- Modern APIs over deprecated ones154- Clear state ownership patterns155- Performance-conscious view composition156- Testable code structure157- No architectural mandates (MVVM/VIPER not required)158- Apple Human Interface Guidelines adherence159160## Reference Files161162All references in `references/`:163- `workflows-review.md` - Review methodology and findings taxonomy164- `workflows-refactor.md` - Refactor methodology and invariants165- `refactor-playbooks.md` - Step-by-step refactor guides166- `state.md` - Property wrappers and ownership patterns167- `navigation.md` - Navigation implementation patterns168- `view-composition.md` - View structure and extraction169- `lists-collections.md` - Identity and ForEach patterns170- `scrolling.md` - Scroll handling and pagination171- `concurrency.md` - Async work with .task172- `performance.md` - Optimization strategies173- `testing-di.md` - Testing and dependency injection174- `patterns.md` - Common SwiftUI patterns175- `modern-swiftui-apis.md` - Legacy API migration176- `code-review-refactoring.md` - Code quality checks177
Full transparency — inspect the skill content before installing.