Skip to content

Lesson 04 · iOS with Swift & SwiftUI

iOS: State, Data Flow & Lifecycle

How data moves through a SwiftUI app: view-local state with @State, two-way bindings with the $ prefix and @Binding, the @Observable macro pattern of a parent-owned model with @Bindable children, computed display values, work at the right moments with onAppear and task, the scenePhase lifecycle with saving on background, and a checklist for deciding where every piece of state lives.

Prerequisites: iOS: SwiftUI Views & Layout

Learning objectives
By the end of this lesson, you will be able to:
  • Declare view-local mutable state with @State, keeping it private and initialized inline because the owning view is its only client
  • Build two-way bindings with the $ prefix and hand writable storage to child views through @Binding and @Bindable
  • Structure a screen around an @Observable model class owned by a parent's @State, sharing one instance across views and deriving display values as computed properties
  • Run work at the right moments with .onAppear for synchronous tasks and .task for auto-cancelled async work
  • Observe the scene lifecycle with @Environment(\.scenePhase) and .onChange, saving model data when the app enters the background