2 min read
SwiftUI Patterns
Drop this into your project to get cleaner, more consistent SwiftUI code. Especially useful for your first few Swift projects — it prevents Claude from overcomplicating things.
Filename: swiftui-patterns.md
The Skill File
Copy this into a file called swiftui-patterns.md in your project root, next to your CLAUDE.md.
# SwiftUI Patterns
Follow these patterns for all SwiftUI code in this project. The goal is clean, modern SwiftUI that's easy to read and maintain — no over-engineering.
## View Composition
- Extract views into separate files when they exceed ~50 lines
- Use ViewBuilder for reusable layout containers
- Keep ContentView as a simple coordinator, not a monolith
## State Management
- Use @State for local view state (toggles, text fields, selections)
- Use @Binding when a child view needs to modify parent state
- Use @Observable (iOS 17+/macOS 14+) for shared app state
- Don't use @ObservableObject/@StateObject unless targeting older OS versions
- Keep state as close to where it's used as possible
## Navigation
- Use NavigationStack (not deprecated NavigationView)
- Use TabView for tab-based navigation
- Present secondary content with .sheet() or .fullScreenCover()
- Use .alert() and .confirmationDialog() for quick user decisions
## Lists & Data
- Always conform data models to Identifiable
- Use ForEach with identifiable data, not index-based
- Use .swipeActions for item-level actions
- Use .searchable for list filtering
## Common Modifiers
- .padding(), .frame(), .cornerRadius(), .shadow()
- .font(), .foregroundColor(), .background()
- .animation(), .transition() for motion
- .onAppear(), .onTapGesture() for interactions
## Persistence
- Use @AppStorage for simple settings (wraps UserDefaults)
- Use SwiftData for structured data with @Model
- Save on meaningful events, not on every keystroke
When to Use This
Add this to any macOS or iOS project. It’s especially helpful for your first few Swift apps — it keeps Claude from mixing old and new patterns.