Swift Academy · Lesson

@Binding: Two-Way Data Flow

Passing mutable state down the view hierarchy with @Binding and $ prefix.

Lesson 1 of 412 steps

@Binding: Two-Way Data Flow is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

`@Binding` creates a two-way connection between a parent's state and a child view, allowing the child to read and write the parent's value without owning it.

Creating a Binding

```swift struct Parent: View { @State private var name = "" var body: some View { ChildView(name: $name) // $ creates Binding } } struct ChildView: View { @Binding var name: String var body: some View { TextField("Name", text: $name) } } ```

The $ Prefix

`$name` on a `@State` property returns a `Binding` — a reference to the parent's storage that allows two-way reads and writes.

Writing Through Binding

```swift struct Toggle: View { @Binding var isOn: Bool var body: some View { Button(isOn ? "On" : "Off") { isOn.toggle() } } } ```

Constant Binding for Previews

```swift #Preview { ChildView(isOn: .constant(true)) // non-mutable binding for preview } ```

Binding to a Collection Element

```swift ForEach($items) { $item in // $items is Binding<[Item]> TextField("Name", text: $item.name) } ```

@State vs @Binding Summary

• `@State` — *owns* the value, lives in view • `@Binding` — *references* another view's state, doesn't own • Changes via `@Binding` propagate back to the owner

Derived Binding

```swift let isLarge: Binding = $fontSize.map( get: { $0 > 18 }, set: { $0 ? 24 : 14 } ) ```

Binding<Optional>

```swift if let $item = $selectedItem { DetailView(item: $item) } ``` Swift 5.7+ supports optional binding with `if let $binding`.

Multiple Bindings

```swift struct Form: View { @State private var name = "" @State private var age = 0 var body: some View { FormFields(name: $name, age: $age) } } ```

Quick Check

What does the `$` prefix on a `@State` property return?

Recap

Key takeaways: • `@Binding var x: T` — read/write reference, doesn't own • `$stateProperty` creates the `Binding` • `.constant(value)` for non-mutable preview bindings • Child writes via `@Binding` update the parent's `@State` • `ForEach($collection) { $element in }` for mutable iteration Next: @ObservableObject.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “@Binding: Two-Way Data Flow” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “@Binding: Two-Way Data Flow”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “@Binding: Two-Way Data Flow”?

Passing mutable state down the view hierarchy with @Binding and $ prefix. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “@Binding: Two-Way Data Flow” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. @Binding: Two-Way Data Flow
  2. @ObservableObject and @StateObject
  3. @EnvironmentObject for Shared State
  4. Single Source of Truth and Unidirectional Flow
← Back to Swift Academy