Differences between @State, @Binding, @ObservedObject, and @EnvironmentObject

Tests SwiftUI state ownership and propagation. @State owns local value types; @Binding borrows for two-way edits; @ObservedObject accepts an external ObservableObject; @EnvironmentObject reads one from the environment.
What's really being asked
This question probes your mental model of SwiftUI's declarative data flow and ownership. Interviewers want to see that you know which wrapper owns the data, which merely borrows it, and how reference-type state differs from value-type state across a view tree.
The full answer
A good answer hits four things in order. First, @State is for local value-type state owned by the view itself; SwiftUI stores it outside the struct and mutates it in place, and it should be private. Second, @Binding is for two-way access to state owned by an ancestor; it does not own the data, it only reads and writes it, making it ideal for custom controls and sheet dismissal. Third, @ObservedObject is for reference-type objects that conform to ObservableObject; the view holds a reference but does not own or create the object, and the parent typically passes it down. Fourth, @EnvironmentObject is for shared ObservableObject instances that are injected higher in the view hierarchy with environmentObject and retrieved implicitly by any descendant that declares the need; this avoids excessive prop drilling. You should also mention that only @State and @Binding work well with value types like structs and enums, while @ObservedObject and @EnvironmentObject require classes.
The mistakes people make
Red flags include saying @State can hold a class, confusing @ObservedObject with a globally shared object, or claiming @EnvironmentObject creates the object automatically. Another mistake is omitting that @Binding is two-way; some candidates describe it as read-only. Also, watch for candidates who say @State is stored inside the view struct; SwiftUI actually stores it in external storage and reinitializes the struct on every update.
What usually comes next
Interviewers often push deeper by asking when to use @StateObject instead of @ObservedObject, how the environment differs from dependency injection, or what happens if an environment object is missing at runtime. They may also ask about performance implications of ObservableObject publishing, or how to avoid unnecessary view redraws using objectWillChange or @Published granularity.
A concrete example
Imagine a toggle in a settings row. The parent view owns a Boolean using @State. It passes a @Binding into the row subview so the row can flip the toggle. The row itself does not own the truth. Meanwhile, a network service class conforming to ObservableObject is created in the root view and passed into a list as an @ObservedObject. Finally, a user session object is placed into the environment at the app root with environmentObject, and a deep profile view reads it via @EnvironmentObject without the intermediate views knowing it exists.
Interview question
A child view receives an ObservableObject from its parent, must update when it publishes changes, and must not manage its lifecycle. Which wrapper fits?
- a.@EnvironmentObject, because the object is shared across the view hierarchy
- b.@Binding, because the parent owns the truth and the child needs to mutate it
- c.@State, because the child needs to hold its own copy of the object
- d.@ObservedObject, because the child receives a reference-type object it does not ownCorrect
Why? this is the answer
@ObservedObject is correct because the child gets an externally created ObservableObject and must react to its updates without taking ownership. @Binding is tempting because the data comes from the parent, but it is meant for value types, not reference-type ObservableObjects.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
- #swiftui
- #state-management
- #property-wrappers
- #data-flow
- #ios
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on swiftui — each one lists the topics its interview covers.
See open roles