@State, @Binding, @Bindable, and (legacy) @ObservedObject

The interviewer asks

“Explain @State, @Binding, @Bindable, and @ObservedObject. When would you use each?”

How to answer

@State is storage the view owns, for a simple value or for an @Observable model the view creates. @Binding is a two-way reference to a value someone else owns, which is how a child edits a parent's @State. @Bindable is the equivalent for an @Observable object you were handed: it lets you write $model.property to get bindings to its properties. @ObservedObject is the older way to watch a passed-in ObservableObject; in new code a passed-in @Observable model is just a plain property.

Key points

  • @State: view-owned source of truth; private; survives re-renders for the view's identity
  • @Binding: no storage of its own; reads and writes the owner's value ($count creates one)
  • @Bindable: wraps an @Observable reference to produce bindings; works on a property or as a local @Bindable var model = model inside body
  • Read-only access to a passed-in @Observable model needs no wrapper: let model: Model
  • @ObservedObject (legacy): passed-in ObservableObject; pairs with @StateObject for ownership

If they dig deeper

Why @State should be private (it is initialized once; values passed in later are ignored). Binding(get:set:) and .constant(...) for previews. Getting bindings from an environment model with a local @Bindable. Value vs reference semantics: @Binding to a struct vs @Bindable on a class.

Common pitfalls

Using @State for data passed in from a parent (it ignores later updates). Using @Binding where the child only reads (pass a plain value). Reaching for @Bindable when no $ binding is needed. Using @ObservedObject for an object the view creates (use @StateObject, or @State with @Observable).

How to use it

swift
@MainActor
@Observable
final class Settings {
    var notificationsOn = false
}

struct Parent: View {
    @State private var count = 0                 // owned value
    @State private var settings = Settings()     // owned model

    var body: some View {
        Counter(count: $count)
        SettingsForm(settings: settings)
    }
}

struct Counter: View {
    @Binding var count: Int                      // edits the parent's value
    var body: some View {
        Button("Count: \(count)") { count += 1 }
    }
}

struct SettingsForm: View {
    @Bindable var settings: Settings             // bindings into the model
    var body: some View {
        Toggle("Notifications", isOn: $settings.notificationsOn)
    }
}

struct EnvironmentToggle: View {
    @Environment(Settings.self) private var settings
    var body: some View {
        @Bindable var settings = settings        // local @Bindable for $
        Toggle("Notifications", isOn: $settings.notificationsOn)
    }
}

Legacy equivalent: class Settings: ObservableObject with @Published, owned by @StateObject and passed as @ObservedObject var settings: Settings, which also gives $settings.notificationsOn.

Best practices

  • @State: private, view-owned values and models the view creates
  • @Binding: a child that must write a parent's value
  • @Bindable: a child that needs bindings into a passed-in @Observable model; plain let otherwise
  • @ObservedObject: only for existing ObservableObject code

Related concepts

  • Observable macro (the full model), @StateObject vs @ObservedObject, ObservableObject and @Published, sharing models via the environment (@EnvironmentObject), optional binding, forms and controls