How do ObservableObject, @Published, @StateObject, @ObservedObject, @State, and @Binding manage SwiftUI state?

Instruction: Use the Combine observation model to show who owns a shared reference model, who observes it, and how a child edits fields through bindings. Distinguish the ObservableObject protocol from property wrappers and the newer @Observable macro.

Context: Assesses SwiftUI state ownership, model lifetime, observation, and bindings using complete parent/child view code.

Updated

Example answer

ObservableObject is a Combine protocol, not a property wrapper. A conforming reference model can use @Published properties to announce changes. SwiftUI's @StateObject owns an instance for the lifetime of a view identity; @ObservedObject observes an instance supplied by another owner. @State stores view-local state, and @Binding provides read/write access to state owned elsewhere.

This example uses the Combine observation model available on iOS 14+ (and macOS 11+):

import SwiftUI
import Combine

@MainActor
final class UserProfile: ObservableObject {
    @Published var name: String
    @Published var bio: String

    init(name: String, bio: String) {
        self.name = name
        self.bio = bio
    }
}

@MainActor
struct ProfileScreen: View {
    @StateObject private var profile = UserProfile(name: "Alex", bio: "Hello")
    @State private var isEditing = false

    var body: some View {
        VStack {
            ProfileSummary(profile: profile)
            Toggle("Edit profile", isOn: $isEditing)
            if isEditing {
                ProfileEditor(name: $profile.name, bio: $profile.bio)
            }
        }
    }
}

@MainActor
struct ProfileSummary: View {
    @ObservedObject var profile: UserProfile
    var body: some View {
        VStack {
            Text(profile.name)
            Text(profile.bio)
        }
    }
}

struct ProfileEditor: View {
    @Binding var name: String
    @Binding var bio: String
    var body: some View {
        VStack {
            TextField("Name", text: $name)
            TextField("Bio", text: $bio)
        }
    }
}

The screen creates one model; the summary borrows it and the editor receives only the fields it edits. Keep UI model mutations on the main actor, avoid recreating owned models inside body, and divide large models by responsibility. A binding is not a separate copy of the model.

The newer Observation system uses the @Observable macro (iOS 17+), with different ownership and binding patterns. Do not combine its spelling with ObservableObject or assume the two systems require identical wrappers.

Sources

ObservableObject; StateObject; ObservedObject; Observation migration.

Related Questions