@Bindable for passed-in models

SwiftUI Mid-level Swift 5.9+ iOS 17+ Reviewed By Alok Singh

Pick the fix

TextField("Name", text: $profile.name) doesn't compile because profile is a plain property. Which fix?

@Observable final class Profile { var name = "" }

struct EditProfile: View {
    var profile: Profile
    var body: some View {
        TextField("Name", text: $profile.name)
    }
}
Options
Hint 1 of 2

Who owns profile?

Hint 2 of 2

Which wrapper is made for Observation?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A @Bindable var profile: Profile

Correct. @Bindable provides bindings to an @Observable object you were handed.

Why not the others?
  • B @State var profile: Profile. The child would take a copy of the reference as its own state and ignore later values from the parent.
  • C @Binding var profile: Profile. That expects a Binding<Profile> from the parent, which isn't what you have.
  • D @ObservedObject var profile: Profile. That needs ObservableObject; an @Observable class doesn't conform.

30-second answer

@Bindable lets a view create bindings ($model.name) to properties of an @Observable model it was given but doesn't own. Use it when a child edits a model with TextField, Toggle or Slider. It doesn't own or keep the object alive; the owner still holds it in @State.

Practise saying it · 30 s. No recording, just a timer

Explain when you need @Bindable.

Say your answer out loud, then check which of these points you covered:

  • Bindings into a model you don't own
  • Owner keeps @State
  • Works with @Observable only

Deep dive

You can also write @Bindable var model = model inside body for a local binding.

For models from the environment: @Environment(Model.self) var model, then @Bindable var model = model in body.

@Bindable works only with @Observable classes.

It replaces @ObservedObject plus $ from the ObservableObject world.

Common mistakes

  • Using @State in the child, which creates a second owner and ignores later values.
  • Using @Binding for a whole model object.
  • Thinking @Bindable keeps the model alive.

Senior take & follow-ups

Seniors keep ownership (@State) and editing access (@Bindable) clearly separate.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you get bindings to an environment model?

    Show hint

    Shadow it with @Bindable inside body.

  2. Does @Bindable re-render the view?

    Show hint

    Observation does, based on what body reads.

Suggest a fix if something here is wrong or out of date.