swiftui-patterns
SwiftUI Patterns
Build modern iOS apps with SwiftUI's declarative syntax, state management, and reactive patterns.
Core Patterns
Basic View
struct ContentView: View {
@State private var count = 0
var body: some View {
VStack {
Text("Count: \(count)")
.font(.largeTitle)
Button("Increment") {
count += 1
}
.buttonStyle(.borderedProminent)
}
.padding()
}
}
ObservableObject
class UserViewModel: ObservableObject {
@Published var users: [User] = []
@Published var isLoading = false
func fetchUsers() async {
isLoading = true
defer { isLoading = false }
do {
users = try await UserService.fetchUsers()
} catch {
print("Error: \(error)")
}
}
}
struct UserListView: View {
@StateObject private var viewModel = UserViewModel()
var body: some View {
List(viewModel.users) { user in
Text(user.name)
}
.task {
await viewModel.fetchUsers()
}
}
}
Custom ViewModifier
struct CardModifier: ViewModifier {
func body(content: Content) -> some View {
content
.padding()
.background(Color.white)
.cornerRadius(10)
.shadow(radius: 5)
}
}
extension View {
func cardStyle() -> some View {
modifier(CardModifier())
}
}
Best Practices
- Use @State for local view state
- Use @StateObject for view models
- Use @ObservedObject for passed objects
- Leverage SwiftUI previews
- Extract reusable components
- Use proper property wrappers
- Implement accessibility
Resources
More from spjoshis/claude-code-plugins
excel-analysis
Master Excel for data analysis with pivot tables, formulas, Power Query, and advanced Excel techniques.
50flutter-performance
Optimize Flutter app performance with widget rebuilds, memory management, rendering optimization, and profiling techniques. Achieve smooth 60fps rendering.
10bloc-pattern
Master BLoC (Business Logic Component) pattern for Flutter with flutter_bloc. Learn events, states, testing, and advanced patterns for scalable apps.
9product-backlog-management
Master product backlog management with prioritization frameworks, refinement techniques, estimation, and continuous backlog optimization for maximum value delivery.
6laravel-development
Master Laravel 11 with Eloquent ORM, routing, middleware, queues, testing, and modern PHP development patterns.
6rxjs-patterns
Master RxJS in Angular with observables, operators, subjects, error handling, and reactive patterns for building responsive applications.
5