Skip to content
tezvyn:

What is @State in SwiftUI and how does it affect the view?

Source: developer.apple.comEasyHow cards are made

What is @State in SwiftUI and how does it affect the view?

Tests declarative data flow ownership. A strong answer defines @State as private value-type storage owned by the view that triggers a body re-evaluation on mutation. Red flag: confusing it with external data sources like @ObservedObject or @Binding.

What's really being asked

Whether you understand the source of truth and ownership model in SwiftUI's declarative architecture. The interviewer is checking that you know why a view struct, which is a value type, can appear to mutate its own state without breaking value semantics. They also want to see that you grasp the difference between state owned by the view versus state owned externally and passed in, because mixing these up leads to architecture bugs and unnecessary view invalidations across the tree.

The full answer

First, define @State as a property wrapper that creates private storage for a value type, owned exclusively by the view that declares it. Second, explain that modifying the wrapped value marks the view as invalid and schedules an asynchronous re-render of the body, but does not mutate the struct instance directly. Third, clarify that SwiftUI manages the actual backing storage outside the view struct, which is why var is permitted even though body is computed inside an immutable struct context. Fourth, note that @State should be private and is intended for simple local UI state like toggle positions, text field input, or animation flags. Fifth, mention that the projected value is a Binding, allowing child views to mutate the value without owning it.

The mistakes people make

Confusing @State with @ObservedObject or @Binding, which are for external or shared data. Claiming that @State persists across app launches or process death. Saying that @State gives a struct reference semantics. Asserting that mutating @State immediately and synchronously redraws the view on the same line of code, rather than invalidating and scheduling a future render pass. Using @State for non-private or globally shared data, which breaks the single source of truth principle.

What usually comes next

When would you use @State versus @ObservedObject or @StateObject? Why does SwiftUI require @State variables to be private? What happens if you pass a @State binding to a child view? How does @State interact with the identity of a view, such as when a view is removed from the hierarchy and later reinserted? Can you use @State with a reference type, and what are the risks?

A concrete example

A simple toggle. Declare @State private var isOn: Bool = false inside a View struct. Bind it to a Toggle("Airplane Mode", isOn: $isOn). When the user taps the toggle, SwiftUI writes to the backing storage, invalidates the view, and recomputes body with the new value, moving the toggle switch to the on position without any imperative UI update code. If the view is removed from the window, SwiftUI can discard the storage, and if it returns with the same identity, the state may be restored depending on the container.

Interview question

What is the immediate effect of mutating a @State property in a SwiftUI view?

  • a.The new value is written to persistent storage to survive process death
  • b.The struct instance mutates in place, giving the view reference semantics
  • c.The view's body recomputes synchronously on the exact next line of code
  • d.SwiftUI marks the view as invalid and schedules an asynchronous body re-evaluationCorrect
Why?

Mutating @State marks the view invalid and schedules an asynchronous re-evaluation of body rather than redrawing immediately. The synchronous redraw distractor is wrong because SwiftUI batches state changes and updates the render tree in a future pass, not on the next line of code.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

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.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on swiftui — each one lists the topics its interview covers.

See open roles