swiftui

SwiftUI

SwiftUI declarative UI framework patterns

Swift
framework
Used by 160 projects

Details

Language / Topic
swiftSwift
Category
framework
Compatible Frameworks
swiftui

Rules

balanced
- Use `@State` for local view state, `@Binding` for parent-child, `@StateObject` for view-owned ObservableObjects, `@EnvironmentObject` for shared state.
- Build views with small, composable `View` structs — extract subviews into separate types when a view exceeds ~30 lines.
- Use `@Observable` (iOS 17+) macro instead of `ObservableObject` protocol — it provides automatic, granular observation without `@Published`.
- Use `.task {}` modifier for async work on view appear — it automatically cancels when the view disappears.
- Use `@State` for view-local state, `@StateObject` for owned observable objects, `@ObservedObject` for injected ones.
- Use `@EnvironmentObject` for dependency injection of shared services across the view hierarchy.
- Keep views small and composable. Extract subviews when a view exceeds ~40 lines.
- Use `NavigationStack` (iOS 16+) with `navigationDestination` for type-safe navigation.
- Use `async`/`await` with `Task` for data fetching in `.task { }` view modifier.