Daily iOS
SwiftUI

@Observable ownership and passing

When working with an iOS 17 @Observable class in SwiftUI views, using "who owns it" and "how is it passed" as the criteria, when do you use @State, a plain property, @Bindable, and @Environment?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

The view that creates it owns it with @State; a view that receives it and only reads uses let/var; a view that receives it and needs bindings ($) uses @Bindable; and sharing across the whole hierarchy uses .environment plus @Environment(Type.self).

Share a question