@State, @Binding, @StateObject, @ObservedObject, @EnvironmentObject — what's the difference?
The gist
These are property wrappers that tell SwiftUI who owns the data and how the view subscribes to it.
@State— a source of truth owned by the view itself. SwiftUI stores the value outside the struct (the view is recreated, the state is not). For value types and, since iOS 17, for@Observableobjects.@Binding— a read/write reference to someone else's@State; created with$. A child mutates the parent's data without owning it.@StateObject— the view owns anObservableObjectmodel: the object is created once (the argument is an autoclosure) and lives as long as the view.@ObservedObject— anObservableObjectpassed in from outside; the view subscribes toobjectWillChangebut does not own the object. Never create it right ininit— it would be recreated on every parent update.@EnvironmentObject— anObservableObjectthat an ancestor put into the environment via.environmentObject(_:); it is visible to all descendants down the tree. If nobody injected it, the app crashes at runtime.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep