SwiftUI State & Data Flow Questions with Answers

13 practice questions on SwiftUI State & Data Flow (iOS (Swift & SwiftUI)) with answers and step-by-step solutions. Try 10 samples below, then practise all 13 free in the AptiNest app.

Sample questions

Question 1Easy

What does "single source of truth" mean in SwiftUI?

  1. The whole app must use exactly one @State property in total
  2. Every view must keep its own copy of shared data
  3. All data must come from one network request
  4. Each piece of data has one owner; others get bindings

Answer: D. Each piece of data has one owner; others get bindings

Explanation

Keeping data in one place and passing bindings or references avoids copies getting out of sync.

Step-by-step solution

  1. Single source of truth means each piece of data has one owner.
  2. Other views get updates through bindings or references.
  3. This prevents multiple copies of the same data existing.
  4. It ensures all views stay in sync with the latest data.
  5. So, each piece of data has one owner; others get bindings.
  6. Final answer: Each piece of data has one owner; others get bindings.

Why the other options are wrong

  • A. The rule is one owner per data item, not one @State for the whole app.
  • B. Copies of shared data break the single source of truth, since they can drift apart.
  • C. Data does not need to come from one network request; the rule is about ownership in views.

Tip: Use @State or @ObservedObject to make data the single source of truth in SwiftUI.

Question 2Easy

What is @State used for in a SwiftUI view?

  1. Configuration read from the Info.plist
  2. Data shared across the whole app and saved to disk automatically
  3. A read-only value passed in by the parent
  4. Value state owned by the view; changes refresh the UI

Answer: D. Value state owned by the view; changes refresh the UI

Explanation

@State stores a value that SwiftUI keeps for the view's lifetime. When it changes, the view's body is recomputed.

Step-by-step solution

  1. @State holds data specific to the view.
  2. Changes to @State trigger UI updates.
  3. The view manages @State internally.
  4. UI refreshes when @State value changes.
  5. This makes views responsive to user input.

Why the other options are wrong

  • A. Info.plist holds app configuration, not view state; @State does not read it.
  • B. Data shared across the app and saved to disk is AppStorage or a model, not @State.
  • C. A read-only value from the parent is a plain let or property, not @State.

Tip: @State keeps UI in sync with data changes.

Question 3Easy

In a child view, what does @Binding var value: Int declare?

  1. A two-way link to state owned elsewhere
  2. A value read from the environment
  3. New state owned and stored by the child view
  4. A constant that can't change after init

Answer: A. A two-way link to state owned elsewhere

Explanation

A Binding doesn't own the data. It reads and writes state that lives elsewhere, usually a parent's @State.

Step-by-step solution

  1. Binding var value: Int links to external state, not owned by child view.
  2. It allows changes in child view to update the parent's state directly.
  3. This creates a two-way connection between views for shared data.
  4. The child view reads and writes the same state from another part of the app.
  5. So @Binding var value: Int declares a two-way link to state owned elsewhere.

Why the other options are wrong

  • B. An environment value comes from @Environment, not from @Binding.
  • C. The child does not own this value; @State creates owned state, not @Binding.
  • D. A constant is a let property; @Binding var can be written, so it is not constant.

Tip: Remember, @Binding shares state without owning it. Use it for two-way communication between views.

Question 4Easy

A parent has @State var isOn = false and shows Toggle("Wi-Fi", isOn: $isOn). What does $isOn pass?

  1. The name of the property as a string
  2. A Binding to read and write the parent's state
  3. A copy of the current value that the toggle can't change
  4. An optional that is nil until the toggle is tapped

Answer: B. A Binding to read and write the parent's state

Explanation

The $ prefix gives the property wrapper's projected value, a Binding. The toggle writes through it to the parent's single source of truth.

Step-by-step solution

  1. $isOn gives the binding for isOn.
  2. The toggle uses this binding to read and write.
  3. Binding allows changes in the parent state.
  4. So $isOn passes a Binding to read and write the parent's state.
  5. Final answer: A Binding to read and write the parent's state.

Why the other options are wrong

  • A. $isOn does not pass the name as text; the $ prefix gives a binding.
  • C. Plain isOn passes a copy; $isOn is a binding, so the toggle can change the parent value.
  • D. $isOn is not an optional; it is a Binding that is always there.

Tip: Use $ to pass bindings for two-way data flow in SwiftUI.

Question 5Medium

What does @Environment(\.colorScheme) var scheme give a view?

  1. A new state value that the view owns
  2. A value SwiftUI passes down the view hierarchy
  3. A binding to a parent view's @State colour property
  4. A value loaded from UserDefaults

Answer: B. A value SwiftUI passes down the view hierarchy

Explanation

The environment carries values like the colour scheme, locale or dismiss action down the tree, so views can read them without passing parameters by hand.

Step-by-step solution

  1. The @Environment modifier reads values from the environment.
  2. SwiftUI passes values like colorScheme down the view hierarchy automatically.
  3. Views use @Environment to access these passed-down values directly.
  4. This means the value comes from SwiftUI's environment, not from state or user defaults.
  5. So the view receives a value SwiftUI passes down the hierarchy.

Why the other options are wrong

  • A. colorScheme is not owned by the view; it is read from the environment, and nothing is created.
  • C. It is not a binding; colorScheme is a read-only system value from the environment.
  • D. colorScheme is not loaded from UserDefaults; SwiftUI supplies it from the system appearance.

Tip: Use @Environment to get values passed down from SwiftUI's environment, not from state or user defaults.

Question 6Medium

With Observation, how can a parent provide an @Observable model to deep subviews without passing it through every initializer?

  1. Use .environment(model) and @Environment(Model.self)
  2. Mark the model with @Binding in the root view of the app
  3. Store the model in a static var on each view
  4. Use @AppStorage to share the model object

Answer: A. Use .environment(model) and @Environment(Model.self)

Explanation

The environment can carry @Observable objects by type. Any descendant can read it with @Environment(Model.self).

Step-by-step solution

  1. Use .environment(model) to pass the model to the environment.
  2. Descendants can access it via @Environment(Model.self).
  3. This avoids passing through every initializer.
  4. The model is available to all deep subviews automatically.
  5. Final answer: Use .environment(model) and @Environment(Model.self)

Why the other options are wrong

  • B. @Binding links one value to a child; it is not the way to share a model across the app root.
  • C. A static var on each view is not passed down the tree to subviews.
  • D. AppStorage stores simple values in UserDefaults, so it cannot share a model object.

Tip: Use environment to share observable models efficiently.

Question 7Medium

With the @Observable macro on a model class, when does a SwiftUI view update?

  1. Only when objectWillChange.send() is called
  2. Only when the model is replaced with a new instance
  3. When a property that its body actually read changes
  4. Whenever any property of the model changes

Answer: C. When a property that its body actually read changes

Explanation

Observation tracks which properties each body reads, so a view only refreshes for changes it depends on. ObservableObject, by contrast, notifies on any @Published change.

Step-by-step solution

  1. SwiftUI views observe properties they actually read in their body.
  2. Only those specific properties trigger a view update when changed.
  3. The @Observable macro tracks which properties are used in the body.
  4. So, the view updates only when those tracked properties change.
  5. This makes the view efficient and responsive to relevant changes.

Why the other options are wrong

  • A. objectWillChange.send() belongs to ObservableObject; @Observable tracks reads automatically.
  • B. Replacing the whole model is not needed; changes to read properties update the views.
  • D. Not every property change refreshes a view; only properties the body reads trigger an update.

Tip: Remember: SwiftUI views refresh only for properties they explicitly use in their body.

Question 8Medium

Why is @State usually declared private?

  1. Public @State is saved to UserDefaults automatically
  2. The view owns it; outsiders shouldn't set it
  3. private makes state updates faster
  4. Non-private @State fails to compile

Answer: B. The view owns it; outsiders shouldn't set it

Explanation

@State is the view's own source of truth. A parent that passes in an initial value doesn't control it after the first render, so private makes the ownership clear.

Step-by-step solution

  1. @State holds data owned by one view and kept while that view stays in the hierarchy.
  2. Keeping it private stops other code from setting it, so the view keeps control of its own state.
  3. Marked answer: option B.

Why the other options are wrong

  • A. @State is not saved to UserDefaults automatically.
  • C. Private does not change how fast state updates run.
  • D. Non-private @State compiles fine.

Tip: Keep @State private; pass data in with parameters and share changes with bindings.

Question 9Hard

A view receives an @Observable model and needs TextField("Name", text: $model.name). What is needed?

  1. Wrap the model in @Binding inside the view
  2. Declare it with @Bindable to get property bindings
  3. Nothing; a plain let model gives $model automatically
  4. Mark the model with @Published so $ works

Answer: B. Declare it with @Bindable to get property bindings

Explanation

@Bindable creates bindings to the mutable properties of an @Observable object passed in from elsewhere. A view that owns it with @State can also use $model.name.

Step-by-step solution

  1. An @Observable model passed in needs bindings.
  2. @Bindable provides $model.name.

Why the other options are wrong

  • A. @Binding is for values passed from a parent.
  • C. A plain let gives no $.
  • D. @Published belongs to ObservableObject.

Tip: @Bindable for $ access.

Question 10Hard

What is wrong with assigning to a @State property directly inside a view's body?

  1. It changes state during an update, risking loops
  2. The change is saved but the view never re-renders
  3. It fails to compile because body is a static property
  4. Nothing; that is the usual way to set an initial value

Answer: A. It changes state during an update, risking loops

Explanation

body should only describe the UI. Changing state while it runs triggers another update during the update. Change state in actions, .onAppear or .task.

Step-by-step solution

  1. Assigning to @State inside view body changes state during an update.
  2. This causes another update, leading to potential loops.
  3. State should be changed in actions, .onAppear or .task.
  4. Changing state in body risks infinite re-renders.
  5. Final answer: It changes state during an update, risking loops.

Why the other options are wrong

  • B. Changing state in body does not stop re-rendering; it can trigger more updates.
  • C. body is an instance property, not static, so the code does not fail to compile.
  • D. Setting initial values belongs in the declaration or an init, not inside body.

Tip: Always change @State in actions or lifecycle modifiers, not directly in the view body.

Practise all 13 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all iOS (Swift & SwiftUI) topics · All topics