SwiftUI State

up:: SwiftUI onboarding

Structs in SwiftUI are lightweight and performant. They get destroyed and re-created all the time. To persist data between those redraws, you use State.

Whenever State changes, view invalidates and redraws.

@Observable removes the need for @StateObject and @ObservedObject.

  • @State: For simple variables
  • @Binding: For two-way data flow (like v-model)