@Binding and the $ prefix

SwiftUI Junior Swift 5.1+ iOS 17+ Reviewed By Alok Singh

Pick the fix

A child Toggle must change the parent's @State var isOn. Which child declaration?

Options
Hint 1 of 1

Who owns isOn?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A @Binding var isOn: Bool, and the parent passes $isOn

Correct. The child reads and writes the parent's value.

Why not the others?
  • B var isOn: Bool, and the parent passes isOn. That passes a copy; the toggle can't change the parent.
  • C @State var isOn: Bool initialized from the parent. That creates a second source of truth that drifts from the parent.
  • D Post a Notification when the toggle changes. It works, but it is indirect and untyped, and it skips SwiftUI's data flow.

30-second answer

@Binding is a two-way reference to a value owned somewhere else, usually a parent's @State. The $ prefix reads a property wrapper's projected value. On @State and @Bindable it gives a Binding; on an @Published property inside an ObservableObject it gives a Combine publisher, Published<Value>.Publisher.

Practise saying it · 30 s. No recording, just a timer

Explain @Binding and the $ prefix.

Say your answer out loud, then check which of these points you covered:

  • Binding is a two-way reference
  • $ reads the projected value
  • @Published's $ is a publisher

Deep dive

Pass $isOn from the parent and declare @Binding var isOn: Bool in the child.

.constant(true) creates a read-only binding for previews.

You can derive bindings: $model.settings.volume or Binding(get:set:) for custom logic.

Inside an ObservableObject, $name is a publisher, not a Binding; in a view, $model.name is a Binding.

Common mistakes

  • Copying the value into the child's own @State.
  • Saying $count on @Published is an AnyPublisher.
  • Overusing Binding(get:set:) where a model method would be clearer.

Senior take & follow-ups

Seniors explain projected values as the general mechanism, which shows they know where $ comes from.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you preview a view that takes a Binding?

    Show hint

    Use .constant(...) or @Previewable @State.

  2. What does $ give on a custom property wrapper?

    Show hint

    Whatever its projectedValue returns.

Suggest a fix if something here is wrong or out of date.