Daily iOS
SwiftUI

SwiftUI state ownership

In a SwiftUI product editing screen, the parent view owns the product model and a child view edits just the price field. Among @State, @Binding, and @Observable, which would you put where?

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 parent view holds the product model in @State, the child editing view borrows just the price field as $product.price through @Binding, and only when the model has to be shared with other screens do you lift it into an @Observable class that the view that created it holds with @State.

Share a question