0Pricing
Swift Academy · Lesson

@Binding: Two-Way Data Flow

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

@Binding: Two-Way Data Flow is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “@Binding: Two-Way Data Flow” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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