Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 46 ยท State and Data Flow

@State and @Binding

Learn how @State owns a value in a SwiftUI view, how the $ prefix makes a Binding, and how child views edit their parent's data without owning it. Includes a runnable Binding model.

A SwiftUI view is a cheap, immutable description that gets re-created whenever something changes. So where does a value like "the text typed so far" or "is the switch on" live? Not in the view struct, because that is thrown away and rebuilt. SwiftUI keeps such values in storage that outlives the struct, and the property wrappers @State and @Binding are how a view connects to it.

The idea underneath is the single source of truth: every piece of data has exactly one owner, and every other view that needs it holds a reference to the owner's value rather than a copy. @State marks the owner. @Binding marks a borrower with read-write access. This lesson explains both, the $ prefix that links them, and the special cases like constant bindings. You met bindings in Buttons, Toggles, Sliders and Pickers; now you will see what they are.

One owner, many borrowers

@State Owns a value. Lives as long as the view's identity.
@Binding Borrows a value. Reads and writes the owner's copy.
$name Turns a state value into a Binding.

Change the owner and every view that reads it updates.

1 / 2

@State: a view owns a value

Declare @State on a property with an initial value. It is almost always private, because it belongs to this view and its subtree.

Swift
struct LikeButton: View {
    @State private var likes = 0

    var body: some View {
        Button("Likes: \(likes)") {
            likes += 1
        }
    }
}

likes += 1 looks like it mutates a struct property inside a non-mutating body. That works because the wrapper's setter writes into storage managed by SwiftUI, not into the struct. When the value changes, SwiftUI marks the view as needing an update and calls body again, and the button redraws with the new count. The next time the view struct is re-created (for example because its parent re-rendered), SwiftUI hands the same storage back, so the count survives.

The initial value is used once, when the view first appears in the tree. Re-creating the struct does not reset it. State is discarded only when the view leaves the tree or its identity changes, a topic of View Identity and Lifecycle.

Use @State for simple, local, view-specific values: a text field's content, whether a sheet is showing, a selected tab. For data shared by many screens, you will use observable models in the next lesson instead.

The $ prefix and Binding

Prefixing a state property with $ gives you its projected value, which for @State is a Binding. A binding is a pair of capabilities: read the current value and write a new one. It stores no value of its own.

Swift
struct SettingsView: View {
    @State private var notificationsOn = true

    var body: some View {
        Toggle("Notifications", isOn: $notificationsOn)
    }
}

Toggle needs to write when the user flips it, so it asks for isOn: Binding<Bool>. You pass $notificationsOn. Without the $ you would pass a plain Bool, a copy that the toggle could read but never change.

To build a reusable child that edits its parent's data, declare the property as @Binding and have the parent pass $value:

Swift
struct VolumeControl: View {
    @Binding var volume: Double

    var body: some View {
        Slider(value: $volume, in: 0...1)
    }
}

struct PlayerView: View {
    @State private var volume = 0.5

    var body: some View {
        VStack {
            Text("Volume: \(volume, format: .percent)")
            VolumeControl(volume: $volume)
        }
    }
}

Notice that VolumeControl has no initial value. It cannot create the data, only borrow it. Inside the child, volume reads the parent's value and $volume passes the same binding further down to the Slider. Moving the slider updates the parent's state, the parent's body re-runs, and the Text shows the new value.

A runnable model

SwiftUI itself cannot run here, but the mechanics are small enough to rebuild. The program below defines a State-like wrapper that keeps its value in a shared box, and a Binding wrapper made of a getter and a setter. Names are reused on purpose, so the shape of the real code is visible.

Swift Building State and Binding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

Both buttons write to the same storage, so after two taps the screen reads 2. In real SwiftUI the storage is kept by the framework and tied to the view's place in the tree, not copied around with the struct, but the data flow is the same.

Derived and constant bindings

Because a binding is just a getter and a setter, you can make new ones from old ones. SwiftUI does this for you:

  • $user.name reaches into a struct and gives a Binding<String> for one field of a @State var user.
  • Binding.constant(true) is a binding whose value never changes and ignores writes. It is ideal for previews and tests where you need a control but no real owner.
  • You can build a custom Binding(get:set:) to transform a value, for example an inverted Boolean or a clamped number.
Swift Derived and constant bindings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

name and age write straight through to the one User in storage, and clampedAge shows how a custom binding can validate a write. The constant binding shows why it is safe in a preview: nothing can change it.

Do not create @State in a child just to copy a value the parent passed in. The copy is initialized once and then drifts from the parent's data. If the child must edit the parent's value, take a @Binding. If the child only reads it, use a plain let.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can a SwiftUI view change a @State property inside a non-mutating body?

    The struct only holds a reference to storage owned by SwiftUI. Writing through the wrapper updates that storage and triggers a new body evaluation.
  2. 2. What is $count when count is declared with @State?

    The dollar prefix accesses the property wrapper's projected value, which for @State is a Binding.
  3. 3. A child view needs to edit a Boolean owned by its parent. What should the child declare?

    A @Binding gives the child read-write access to the parent's single source of truth without owning it.
  4. 4. When is Binding.constant(true) most useful?

    A constant binding supplies a fixed value and ignores writes, so you can render a control without wiring up real state.

Summary

  • @State makes a view the owner of a value; keep it private and use it for simple local data.
  • The initial value is used once; state survives struct re-creation and resets only when the view's identity ends.
  • $name gives the projected value, a Binding: a getter and setter with no storage of its own.
  • A child takes @Binding to edit data it does not own; the parent passes $value.
  • Bindings can be derived ($user.name), custom (Binding(get:set:)) or constant (Binding.constant).

Next, share richer data between many views with @Observable and Observation.