Real-world scenario: Your expense split app list is re-rendering constantly, draining battery. Use Self._printChanges() to find the culprit property causing unnecessary redraws.
var body: some View {
let _ = Self._printChanges()
ExpenseListView(expenses: expenses, totalAmount: total)
}
// Output: ExpenseListView: @State expenses changed.
Real-world scenario: Sleep tracker showing lag when scrolling through 100+ sleep entries. Use SwiftUI Instruments template to identify excessive body calls.
@State vs @StateObject Bug That Cost Me 2 Hours: Memory Leaks in SwiftUIReal-world scenario: Fitness app creating new HealthKit manager instances on every view update, causing memory to spike from 50MB to 300MB.
URLProtocol for Request InspectionReal-world scenario: API calls failing in production but working in dev. Implement custom URLProtocol to log all requests in-app.
po Commands That Saved My Debugging SessionReal-world scenario: App crashes when user taps notification. Using po UIApplication.shared.keyWindow?.rootViewController to inspect view hierarchy in lldb.
Real-world scenario: Expense split app with Core Data fetch requests scattered across 5 different views. Refactor to repository pattern for testability.
Real-world scenario: Managing complex navigation in fitness app: onboarding → home → workout detail → workout in progress → workout complete.
Real-world scenario: Switching between mock and real HealthKit managers for testing. Using @EnvironmentObject and protocol-based injection.
Real-world scenario: Sleep tracker’s “analyze sleep quality” feature has 200+ lines of business logic in ViewModel. Extract to Interactor.
Real-world scenario: 15 different views need access to analytics and network managers. Implement lightweight service locator vs passing through environment.
Real-world scenario: User onboarding with 5 steps, each step can fail, skip, or complete. Using enum-based state machine.
.task() Modifier That Replaced My onAppear + .onDisappear Cleanup CodeReal-world scenario: Fetching user’s expenses when view appears and cancelling network request on disappear. Task handles both automatically.
Real-world scenario: Every card in expense app has same shadow, corner radius, padding. Create .cardStyle() modifier.
@AppStorage Trick for Feature Flags Without BackendReal-world scenario: A/B testing new expense split algorithm. Toggle between old/new logic using AppStorage.
Real-world scenario: Custom tab bar that adjusts height based on longest tab title. Use PreferenceKey to bubble up height from children.
GeometryReader Hack for Responsive Design (Without Breaking Layout)Real-world scenario: Sleep tracker charts need to resize for iPhone SE vs iPhone 15 Pro Max. Using GeometryReader without causing layout issues.
Real-world scenario: Expense list with 500+ items causes memory spike. Implement LazyVStack with pagination and prefetching.
.onChange Modifier: Replacing Combine Subscriptions in SwiftUIReal-world scenario: Update chart when date range changes. Using .onChange(of: dateRange) instead of Combine sink.
AnimatableModifier That Made My Charts SmoothReal-world scenario: Bar chart bars jump to new values. Implement AnimatableModifier for smooth transitions.
Real-world scenario: Sleep tracker needs sleep data, heart rate, steps. Batching permissions vs one-by-one UX considerations.
Real-world scenario: User tracks sleep with Apple Watch. App needs to fetch new sleep data at 8 AM daily for analysis.
Real-world scenario: User splits expense on iPhone, expects to see it on iPad immediately. Handling merge conflicts.
Result Type: Cleaner Error Handling Than do-catch EverywhereReal-world scenario: Network layer with 10 API endpoints. Using Result<T, APIError> for consistent error handling.
Real-world scenario: Sleep tracker’s core feature needs sleep data, but user denies permission. Fallback to manual entry.
Real-world scenario: UserDefaults.standard.string(forKey: "userId")! crashes when user clears app data. Defensive programming.
Real-world scenario: Testing expense split calculation displayed correctly in view without slow UI tests.
Real-world scenario: Replacing completion handlers in networking layer with async/await. Before/after comparison.
Real-world scenario: Prompting after user successfully splits 3rd expense (moment of value) vs random prompt on app open.
Real-world scenario: New sleep analysis algorithm. Roll out to 10% of users first, monitor crash rate before 100% rollout.
Real-world scenario: 40% of users abandon expense split on “Add Friends” screen. Analytics revealed confusing UI copy.