bhylu.com

Advertisements

Swift State and Binding

Keep one source of truth and let a child edit it through an explicit binding. Requires Mac/Xcode; this Apple-only source was not SDK-compiled or run here.

How it works

State stores view-owned transient data. Binding points to storage owned elsewhere; it does not create another independent copy. The dollar projection passes a binding to controls or child views. Put lasting business rules in a model rather than assuming a Stepper range validates every possible input source.

Apple-only setup and review status

Create a separate iOS SwiftUI App learning project in a suitable Xcode version, choose an appropriate simulator/device and replace the generated app/view Swift source with this complete file. Keep one @main entry point. Widget and App Clip lessons explicitly require their own extension/clip targets instead; capability, purpose-string, entitlement and account steps in the lesson must be completed separately. No Xcode project, signing certificate, account or service was provisioned here.

Bhylu has no Swift browser runner. The JavaScript/Python editors do not run these sources; Apache, PHP and MySQL are not required. Downloads are source text, never executable teaching endpoints.

Download the complete state-binding.swift · Download all source files with hash checks

For the optional collection, save download-examples.mjs in a new folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/swift/. It refuses an existing bhylu-swift-examples folder and installs or executes no Swift automatically. Apple-only files remain separate Xcode learning examples, not a portable Swift package target.

Example

state-binding.swift

// Apple-only learning file: iOS 17+ target with a suitable Xcode SDK.
// Not compiled or run in this Windows/Linux review.
import SwiftUI

struct PlaceEditor: View {
    @Binding var places: Int
    var body: some View { Stepper("Places", value: $places, in: 1...6) }
}

struct DemoView: View {
    @State private var places = 1
    var body: some View {
        VStack(spacing: 20) {
            Text("Draft places: \(places)")
            PlaceEditor(places: $places)
            Text("Not booked or saved.").font(.caption)
        }.padding()
    }
}

@main
struct WorkshopDemoApp: App {
    var body: some Scene { WindowGroup { DemoView() } }
}

Expected behaviour and manual checks

Manual check: increment and decrement in the child and confirm the parent label updates. The range is one through six. Leaving the view or terminating the process need not preserve this draft. Not executed here.

The parent owns places and the child receives only the capability to edit that value. There is no duplicate state to synchronise by hand. A Binding is a UI data-flow tool, not a concurrency lock or database transaction.

Primary references: The Swift Programming Language and Apple framework documentation. Use the selected platform’s current requirements. Fictional workshop examples do not establish production safety or deployment approval.

Advertisements