skip to content

View-Owned Values and Bindings

@State gives a view a value SwiftUI stores outside the struct, and @Binding lends write access to a child. Interviewers ask why the initial value is read once and who owns the truth.

on this pageshow

explore

questions

5

In SwiftUI, how does a child view edit a value its parent owns in @State, and what does the $ prefix give you?

level: juniorimportance: must knowfreq 80%

answer

  1. two-way connection, no own storage
  2. projectedValue of the state
  3. child declares @Binding var
  4. parent passes $minutes, not minutes

basics

~20 s

The child declares a @Binding property and the parent passes $value, the state's projectedValue, which is a Binding: a read-write reference to the parent's storage with no storage of its own, so the child's writes update the parent's state.

solid answer

~40 s

The parent stays the source of truth, for example `@State private var minutes = 25` on a focus-timer screen. The child, say a duration picker, declares `@Binding var minutes: Int` with no initial value, and the parent creates it with `DurationPicker(minutes: $minutes)`. The `$` prefix asks a property wrapper for its `projectedValue`; for `@State` that is a `Binding<Int>` pointing at the parent's storage. Reading the binding reads the parent's value, writing it writes the parent's value, and every view that reads `minutes` updates. Passing plain `minutes` to a child that declares a plain property gives it a read-only copy that still refreshes when the parent changes it. `$` on a binding yields a binding again, so it can pass through several levels, and `$settings.minutes` binds to one field of a struct held in state.

code

swift · 20 lines
swift
import SwiftUI

struct FocusScreen: View {
    @State private var minutes = 25

    var body: some View {
        VStack {
            Text("Session: \(minutes) min")
            DurationPicker(minutes: $minutes)
        }
    }
}

struct DurationPicker: View {
    @Binding var minutes: Int

    var body: some View {
        Stepper("Duration: \(minutes) min", value: $minutes, in: 5...90, step: 5)
    }
}

go deeper

for a junior

Recall the pattern: parent owns with @State, child declares @Binding without a default, parent passes $value, and the child's writes land in the parent.

for a middle

Explain that $ asks for projectedValue, that a binding has no storage, that $ on a binding returns the binding, and how key-path bindings reach one field of a struct.

for a senior

Spot duplicated sources of truth in review, such as a child copying a binding into @State, and choose between a value, a binding and an event closure for each child.

for a principal

Argue for a team convention on data flow: which values children may edit through bindings, which they only report as events, and how that keeps ownership readable in large screens.

## The problem bindings solve In SwiftUI a value should have **one source of truth**: one place that owns it. On a focus-timer screen, the chosen duration belongs to the screen, because the countdown, the summary label and a reset button all need it. But the control that edits it, a duration picker, is a separate child view. The child needs to **read and write** a value it does not own, without becoming a second owner. That is what `Binding` is for. Apple describes it as a property wrapper that can read and write a value owned by a source of truth, a **two-way connection** between the storage and a view that displays and changes it. A binding stores no data of its own; it refers to storage that lives somewhere else. ## How the pieces fit 1. The parent owns the value: `@State private var minutes = 25`. 2. The child declares what it needs: `@Binding var minutes: Int`, with **no initial value**, because the value is supplied by whoever creates the child. 3. The parent passes the binding: `DurationPicker(minutes: $minutes)`. 4. Inside the child, `minutes` reads and writes like an ordinary property; a `Stepper` or `Picker` takes `$minutes`, which on a binding is the binding itself. The `$` prefix is Swift's syntax for a property wrapper's **`projectedValue`**. For `State`, the projected value is a `Binding<Value>` to the underlying storage. For `Binding`, the projected value is the binding itself, which is why a binding can be handed through any number of view levels. ## Bindings to part of a value `Binding` supports dynamic member lookup with writable key paths, so you can bind to one field of a struct held in state: - `$session.minutes` gives a `Binding<Int>` into a `@State private var session: FocusSession`. - When the value is a mutable collection, `Binding` itself acts as a collection of element bindings (since iOS 15), which is what `ForEach($items) { $item in ... }` uses. - `Binding` also has an initializer that unwraps an optional binding, `Binding(_:)`, which returns `nil` when the underlying value is `nil`. ## Value, binding or state: choosing the child's property | Child declares | What it gets | Child can write? | Updates when the parent's value changes? | |---|---|---|---| | `let minutes: Int` | A copy of the current value | No | Yes, the parent rebuilds the child with the new value | | `@Binding var minutes: Int` | A reference to the parent's storage | Yes, the write lands in the parent | Yes | | `@State private var minutes` | Its own storage | Yes, but only its own copy | No, it becomes a second source of truth | The last row is the common mistake: a child that copies the parent's value into its own `@State` edits a private copy, and the parent never sees the change. ## What a binding is not A binding is easy to over-read, so it helps to state its limits: - It is **not storage**: if the owner goes away, the binding has nothing of its own to fall back on. - It is **not a copy**: two children holding bindings to the same state always see the same value. - It is **not an observer of reference models**: for an `@Observable` class you pass the reference itself, and only create bindings to its properties where a control needs one. ## Reacting to the change in the parent The parent does not need to do anything for display: any view whose `body` reads `minutes` updates automatically. When the parent must run a **side effect** because the value changed, such as rescheduling a reminder, it uses `onChange(of:initial:_:)`, available since iOS 17, whose action can receive the old and new values. The one-parameter `onChange(of:perform:)` form is deprecated since iOS 17. Apple notes the action may run on the main actor, so long-running work belongs elsewhere. ## Review checklist for a child that edits parent data - The child's property is `@Binding var`, not `@State`, and has no default value. - The parent passes `$minutes`, not `minutes`; passing the plain value does not compile against a `Binding` parameter. - The binding is created from the owner, not stored in a separate model to be synced back by hand. - If the child only reports an event ("start tapped"), a closure parameter may be clearer than a binding; bindings are for a value the child edits. Models that are reference types use different tools: an `@Observable` class is passed as a plain reference, and bindings into its properties come from `@Bindable`, which the lesson on Observation-based models covers.

  • How should the parent run a side effect when the child changes the bound value?
    Use `onChange(of:initial:_:)`, available since iOS 17, on a view in the parent: `.onChange(of: minutes) { oldValue, newValue in ... }`. Display needs nothing extra, because any `body` that reads `minutes` updates on its own. The one-parameter `onChange(of:perform:)` is deprecated since iOS 17, and Apple notes the action may run on the main actor, so avoid long-running work inside it.
  • Can a child bind to one field of a struct the parent holds in @State?
    Yes. `Binding` supports dynamic member lookup with writable key paths, so `$session.minutes` yields a `Binding<Int>` into the struct stored in state. Writing through it replaces that field in the parent's stored value. For arrays, `ForEach($items) { $item in ... }` gives each row its own element binding.
  • When is a closure parameter a better fit than a binding for a child view?
    When the child reports an event rather than editing a value, such as a start button that says it was tapped. A binding is a contract over one value both sides read and write; a closure like `onStart: () -> Void` lets the parent decide what the event means without exposing its storage.

A binding is like sharing edit access to a document instead of emailing a copy: the colleague edits the one original, and nothing is kept on their side.

  • A @Binding property stores its own copy of the parent's value.
  • Passing minutes instead of $minutes still lets the child write to it.
  • The child should copy the parent's value into its own @State and edit that.
  • A @Binding property needs an initial value at its declaration.
  • The $ prefix creates a new, independent piece of state for the child.
open as a page

In SwiftUI, why does a view use @State for a value it changes, instead of a plain var stored property on the view struct?

level: juniorimportance: must knowfreq 85%

basics

~20 s

A SwiftUI view is a short-lived struct whose body cannot mutate it. @State keeps the value in storage SwiftUI manages outside the struct, so writes survive the struct being recreated and update the views that read the value.

open as a page

In SwiftUI, when do you create a binding with Binding(get:set:) or Binding.constant(_:) instead of passing a $ projection?

level: middleimportance: should knowfreq 40%

basics

~20 s

Binding(get:set:) adapts a source of truth whose stored shape differs from what a control needs, such as minutes over state kept in seconds. Binding.constant(_:) fills a binding parameter with a fixed value that ignores writes, typically in previews.

open as a page

In SwiftUI, a focus timer's DurationPicker seeds its @State from a preset parameter in init; why does it keep showing the first preset after the parent passes a new one?

level: seniorimportance: should knowfreq 60%

basics

~20 s

SwiftUI keeps @State storage for as long as the view keeps its identity and applies an initial value only when it first creates that storage. Later inits that pass a new preset are ignored, so the picker keeps editing its own stale copy.

open as a page

In SwiftUI built with Xcode 27, how does the State() macro change when a class-typed @State default value, such as a timer engine, is created?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

Built with Xcode 26 or earlier, the @State property wrapper evaluates its default value every time SwiftUI initializes the view and keeps only the first. Built with Xcode 27, the State() macro creates a class-typed default once, the first time.

open as a page