ERP/.shared/ui-ux-pro-max/data/stacks/swiftui.csv
2026-01-03 19:18:40 +08:00

11 KiB

1NoCategoryGuidelineDescriptionDoDon'tCode GoodCode BadSeverityDocs URL
21ViewsUse struct for viewsSwiftUI views are value typesstruct MyView: Viewclass MyView: Viewstruct ContentView: View { var body: some View }class ContentView: ViewHighhttps://developer.apple.com/documentation/swiftui/view
32ViewsKeep views small and focusedSingle responsibility for each viewExtract subviews for complex layoutsLarge monolithic viewsExtract HeaderView FooterView500+ line View structMedium
43ViewsUse body computed propertybody returns the view hierarchyvar body: some View { }func body() -> some Viewvar body: some View { Text("Hello") }func body() -> TextHigh
54ViewsPrefer composition over inheritanceCompose views using ViewBuilderCombine smaller viewsInheritance hierarchiesVStack { Header() Content() }class SpecialView extends BaseViewMedium
65StateUse @State for local stateSimple value types owned by view@State for view-local primitives@State for shared data@State private var count = 0@State var sharedData: ModelHighhttps://developer.apple.com/documentation/swiftui/state
76StateUse @Binding for two-way dataPass mutable state to child views@Binding for child input@State in child for parent data@Binding var isOn: Bool$isOn to pass bindingMediumhttps://developer.apple.com/documentation/swiftui/binding
87StateUse @StateObject for reference typesObservableObject owned by view@StateObject for view-created objects@ObservedObject for owned objects@StateObject private var vm = ViewModel()@ObservedObject var vm = ViewModel()Highhttps://developer.apple.com/documentation/swiftui/stateobject
98StateUse @ObservedObject for injected objectsReference types passed from parent@ObservedObject for injected dependencies@StateObject for injected objects@ObservedObject var vm: ViewModel@StateObject var vm: ViewModel (injected)Highhttps://developer.apple.com/documentation/swiftui/observedobject
109StateUse @EnvironmentObject for shared stateApp-wide state injection@EnvironmentObject for global stateProp drilling through views@EnvironmentObject var settings: SettingsPass settings through 5 viewsMediumhttps://developer.apple.com/documentation/swiftui/environmentobject
1110StateUse @Published in ObservableObjectAutomatically publish property changes@Published for observed propertiesManual objectWillChange calls@Published var items: [Item] = []var items: [Item] { didSet { objectWillChange.send() } }Medium
1211ObservableUse @Observable macro (iOS 17+)Modern observation without Combine@Observable class for view modelsObservableObject for new projects@Observable class ViewModel { }class ViewModel: ObservableObjectMediumhttps://developer.apple.com/documentation/observation
1312ObservableUse @Bindable for @ObservableCreate bindings from @Observable@Bindable var vm for bindings@Binding with @Observable@Bindable var viewModel$viewModel.name with @ObservableMedium
1413LayoutUse VStack HStack ZStackStandard stack-based layoutsStacks for linear arrangementsGeometryReader for simple layoutsVStack { Text() Image() }GeometryReader for vertical listMediumhttps://developer.apple.com/documentation/swiftui/vstack
1514LayoutUse LazyVStack LazyHStack for listsLazy loading for performanceLazy stacks for long listsRegular stacks for 100+ itemsLazyVStack { ForEach(items) }VStack { ForEach(largeArray) }Highhttps://developer.apple.com/documentation/swiftui/lazyvstack
1615LayoutUse GeometryReader sparinglyOnly when needed for sizingGeometryReader for responsive layoutsGeometryReader everywhereGeometryReader for aspect ratioGeometryReader wrapping everythingMedium
1716LayoutUse spacing and padding consistentlyConsistent spacing throughout appDesign system spacing valuesMagic numbers for spacing.padding(16) or .padding().padding(13), .padding(17)Low
1817LayoutUse frame modifiers correctlySet explicit sizes when needed.frame(maxWidth: .infinity)Fixed sizes for responsive content.frame(maxWidth: .infinity).frame(width: 375)Medium
1918ModifiersOrder modifiers correctlyModifier order affects renderingBackground before padding for full coverageWrong modifier order.padding().background(Color.red).background(Color.red).padding()High
2019ModifiersCreate custom ViewModifiersReusable modifier combinationsViewModifier for repeated stylingDuplicate modifier chainsstruct CardStyle: ViewModifier.shadow().cornerRadius() everywhereMediumhttps://developer.apple.com/documentation/swiftui/viewmodifier
2120ModifiersUse conditional modifiers carefullyAvoid changing view identityif-else with same view typeConditional that changes view identityText(title).foregroundColor(isActive ? .blue : .gray)if isActive { Text().bold() } else { Text() }Medium
2221NavigationUse NavigationStack (iOS 16+)Modern navigation with type-safe pathsNavigationStack with navigationDestinationNavigationView for new projectsNavigationStack { }NavigationView { } (deprecated)Mediumhttps://developer.apple.com/documentation/swiftui/navigationstack
2322NavigationUse navigationDestinationType-safe navigation destinations.navigationDestination(for:)NavigationLink(destination:).navigationDestination(for: Item.self)NavigationLink(destination: DetailView())Medium
2423NavigationUse @Environment for dismissProgrammatic navigation dismissal@Environment(\.dismiss) var dismisspresentationMode (deprecated)@Environment(\.dismiss) var dismiss@Environment(\.presentationMode)Low
2524ListsUse List for scrollable contentBuilt-in scrolling and stylingList for standard scrollable contentScrollView + VStack for simple listsList { ForEach(items) { } }ScrollView { VStack { ForEach } }Lowhttps://developer.apple.com/documentation/swiftui/list
2625ListsProvide stable identifiersUse Identifiable or explicit idIdentifiable protocol or id parameterIndex as identifierForEach(items) where Item: IdentifiableForEach(items.indices, id: \.self)High
2726ListsUse onDelete and onMoveStandard list editingonDelete for swipe to deleteCustom delete implementation.onDelete(perform: delete).onTapGesture for deleteLow
2827FormsUse Form for settingsGrouped input controlsForm for settings screensManual grouping for formsForm { Section { Toggle() } }VStack { Toggle() }Lowhttps://developer.apple.com/documentation/swiftui/form
2928FormsUse @FocusState for keyboardManage keyboard focus@FocusState for text field focusManual first responder handling@FocusState private var isFocused: BoolUIKit first responderMediumhttps://developer.apple.com/documentation/swiftui/focusstate
3029FormsValidate input properlyShow validation feedbackReal-time validation feedbackSubmit without validationTextField with validation stateTextField without error handlingMedium
3130AsyncUse .task for async workAutomatic cancellation on view disappear.task for view lifecycle asynconAppear with Task.task { await loadData() }onAppear { Task { await loadData() } }Mediumhttps://developer.apple.com/documentation/swiftui/view/task(priority:_:)
3231AsyncHandle loading statesShow progress during async operationsProgressView during loadingEmpty view during loadif isLoading { ProgressView() }No loading indicatorMedium
3332AsyncUse @MainActor for UI updatesEnsure UI updates on main thread@MainActor on view modelsManual DispatchQueue.main@MainActor class ViewModelDispatchQueue.main.asyncMedium
3433AnimationUse withAnimationAnimate state changeswithAnimation for state transitionsNo animation for state changeswithAnimation { isExpanded.toggle() }isExpanded.toggle()Lowhttps://developer.apple.com/documentation/swiftui/withanimation(_:_:)
3534AnimationUse .animation modifierApply animations to views.animation(.spring()) on viewManual animation timing.animation(.easeInOut)CABasicAnimation equivalentLow
3635AnimationRespect reduced motionCheck accessibility settingsCheck accessibilityReduceMotionIgnore motion preferences@Environment(\.accessibilityReduceMotion)Always animate regardlessHigh
3736PreviewUse #Preview macro (Xcode 15+)Modern preview syntax#Preview for view previewsPreviewProvider protocol#Preview { ContentView() }struct ContentView_Previews: PreviewProviderLow
3837PreviewCreate multiple previewsTest different states and devicesMultiple previews for statesSingle preview only#Preview("Light") { } #Preview("Dark") { }Single preview configurationLow
3938PreviewUse preview dataDedicated preview mock dataStatic preview dataProduction data in previewsItem.preview for previewFetch real data in previewLow
4039PerformanceAvoid expensive body computationsBody should be fast to computePrecompute in view modelHeavy computation in bodyvm.computedValue in bodyComplex calculation in bodyHigh
4140PerformanceUse Equatable viewsSkip unnecessary view updatesEquatable for complex viewsDefault equality for all viewsstruct MyView: View EquatableNo Equatable conformanceMedium
4241PerformanceProfile with InstrumentsMeasure before optimizingUse SwiftUI InstrumentsGuess at performance issuesProfile with InstrumentsOptimize without measuringMedium
4342AccessibilityAdd accessibility labelsDescribe UI elements.accessibilityLabel for contextMissing labels.accessibilityLabel("Close button")Button without labelHighhttps://developer.apple.com/documentation/swiftui/view/accessibilitylabel(_:)-1d7jv
4443AccessibilitySupport Dynamic TypeRespect text size preferencesScalable fonts and layoutsFixed font sizes.font(.body) with Dynamic Type.font(.system(size: 16))High
4544AccessibilityUse semantic viewsProper accessibility traitsCorrect accessibilityTraitsWrong semantic meaningButton for actions Image for displayImage that acts like buttonMedium
4645TestingUse ViewInspector for testingThird-party view testingViewInspector for unit testsUI tests onlyViewInspector assertionsOnly XCUITestMedium
4746TestingTest view modelsUnit test business logicXCTest for view modelSkip view model testingTest ViewModel methodsNo unit testsMedium
4847TestingUse preview as visual testPreviews catch visual regressionsMultiple preview configurationsNo visual verificationPreview different statesSingle preview onlyLow
4948ArchitectureUse MVVM patternSeparate view and logicViewModel for business logicLogic in ViewObservableObject ViewModel@State for complex logicMedium
5049ArchitectureKeep views dumbViews display view model stateView reads from ViewModelBusiness logic in Viewview.items from vm.itemsComplex filtering in ViewMedium
5150ArchitectureUse dependency injectionInject dependencies for testingInitialize with dependenciesHard-coded dependenciesinit(service: ServiceProtocol)let service = RealService()Medium