mental model

// Meaning SwiftUI can observe this class
class A: ObservableObject

// Meaning changes to this property are broadcast to observers
@Published var b

ParentView {

    // View-owned state
    @State var count = 0

    // Creates and owns an instance of class A
    @StateObject var vm = A()

    // Initializes ChildView
}

ChildView {

    // Observes the object owned by the StateObject in ParentView
    @ObservedObject var vm: A
}

@State

So state is simple, it's the view-owned variable. Since SwiftUI views are structs and are constantly destroyed and recreated, SwiftUI stores the actual value somewhere else and reconnects it back to the view whenever the view is recreated.


@Binding

Binding is basically used to access and modify a state variable owned by a parent view. The child doesn't own the data, it just gets a reference to the parent's state so both are working with the same value.


ObservableObject

ObservableObject is basically a protocol that says this class can be observed by SwiftUI. It doesn't itself publish changes, but it allows SwiftUI to observe the class and react when published properties inside it change.


@Published

Published is basically saying:

"Whenever this variable changes, broadcast that change."

So if a property marked with @Published changes, SwiftUI gets notified and can update any views that are observing the object.


@StateObject

StateObject is basically used when the current view creates and owns an object that conforms to ObservableObject.

It's saying: