Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

State & Observable

In a SwiftUI app, every data value or object that can change needs a single source of truth and a mechanism to enable views to change or observe it. Property wrappers like @State and @Observable enable you to declare how each view interacts with mutable data.

Pre-iOS 17

Before iOS 17, @State and @Binding could only be used with value properties, and you’d use @StateObject and @ObservedObject for class objects. To make a class object observable, you’d declare it like this:

class MyViewModelClass: ObservableObject {
  @Published var modelArray: [MyModel] = []
}

Using iOS 17 Observation Framework

An iOS 17 or later app can adopt the Observation framework. Observation:

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo 1