swift-refactor
29
总安装量
5
周安装量
#12856
全站排名
安装命令
npx skills add https://github.com/pproenca/dot-skills --skill swift-refactor
Agent 安装分布
claude-code
5
amp
4
gemini-cli
4
github-copilot
4
codex
4
kimi-cli
4
Skill 文档
Swift Refactor â Code Quality & Modernization
Comprehensive guide for refactoring Swift and SwiftUI code. Contains 42 rules across 7 categories covering API modernization, state architecture, view decomposition, navigation refactoring, architecture patterns, type safety, and Swift fundamentals.
When to Apply
Reference these guidelines when:
- Migrating from deprecated SwiftUI APIs (ObservableObject, NavigationView, old onChange)
- Restructuring state management to reduce re-renders
- Decomposing large views into maintainable components
- Refactoring navigation to use NavigationStack and NavigationPath
- Improving architecture with protocol dependencies and Environment keys
- Strengthening type safety with tagged identifiers and Result types
- Writing idiomatic Swift with proper naming, optionals, and closures
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | API Modernization | CRITICAL | api- |
| 2 | State Architecture | CRITICAL | state- |
| 3 | View Decomposition | HIGH | view- |
| 4 | Navigation Refactoring | HIGH | nav- |
| 5 | Architecture Patterns | HIGH | arch- |
| 6 | Type Safety & Protocols | MEDIUM-HIGH | type- |
| 7 | Swift Language Fundamentals | MEDIUM | swift- |
Quick Reference
1. API Modernization (CRITICAL)
api-observable-macro– Migrate ObservableObject to @Observable macroapi-navigationstack-migration– Replace NavigationView with NavigationStackapi-onchange-signature– Migrate to new onChange signatureapi-environment-object-removal– Replace @EnvironmentObject with @Environmentapi-alert-confirmation-dialog– Migrate Alert to confirmationDialog APIapi-list-foreach-identifiable– Replace id: .self with Identifiable conformanceapi-toolbar-migration– Replace navigationBarItems with toolbar modifier
2. State Architecture (CRITICAL)
state-scope-minimization– Minimize state scope to nearest consumerstate-derived-over-stored– Use computed properties over redundant @Statestate-binding-extraction– Extract @Binding to isolate child re-rendersstate-remove-observation– Remove unnecessary @ObservedObjectstate-onappear-to-task– Replace onAppear closures with .task modifierstate-stateobject-placement– Move @StateObject to app root for shared state
3. View Decomposition (HIGH)
view-extract-subviews– Extract subviews for compositionview-eliminate-anyview– Replace AnyView with @ViewBuilder or genericsview-computed-to-struct– Convert computed view properties to struct viewsview-modifier-extraction– Extract repeated modifiers into custom ViewModifiersview-conditional-content– Use Group or conditional modifiers over conditional viewsview-preference-keys– Replace callback closures with PreferenceKeyview-body-complexity– Reduce view body to under 30 lines
4. Navigation Refactoring (HIGH)
nav-centralize-destinations– Centralize navigationDestination at stack rootnav-value-based-links– Replace destination-based NavigationLink with value-basednav-path-state-management– Use NavigationPath for programmatic navigationnav-split-view-adoption– Use NavigationSplitView for multi-column layoutsnav-sheet-item-pattern– Replace boolean sheet triggers with item binding
5. Architecture Patterns (HIGH)
arch-viewmodel-elimination– Eliminate unnecessary view modelsarch-protocol-dependencies– Extract protocol interfaces for external dependenciesarch-environment-key-injection– Use Environment keys for service injectionarch-feature-module-extraction– Extract features into independent modulesarch-model-view-separation– Separate model logic from view code
6. Type Safety & Protocols (MEDIUM-HIGH)
type-tagged-identifiers– Replace String IDs with tagged typestype-result-over-optionals– Use Result type over optional with error flagtype-phantom-types– Use phantom types for compile-time state machinestype-force-unwrap-elimination– Eliminate force unwraps with safe alternatives
7. Swift Language Fundamentals (MEDIUM)
swift-let-vs-var– Use let for constants, var for variablesswift-structs-vs-classes– Prefer structs over classesswift-camel-case-naming– Use camelCase naming conventionswift-string-interpolation– Use string interpolation for dynamic textswift-functions-clear-names– Name functions and parameters for clarityswift-for-in-loops– Use for-in loops for collectionsswift-optionals– Handle optionals safely with unwrappingswift-closures– Use closures for inline functions
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions – Category structure and impact levels
- Rule template – Template for adding new rules
Reference Files
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |