bhylu.com

Advertisements

Swift MVVM Architecture

Keep domain changes in a model and let the view describe their visible state. Requires Mac/Xcode; this Apple-only source was not SDK-compiled or run here.

How it works

MVVM separates model data, presentation state and view composition; it is not a requirement to create a wrapper class for every small view. This iOS 17+ example uses an observable main-actor model to own checked local booking state. It intentionally has no server, persistence or authentication layer.

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 mvvm.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

mvvm.swift

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

@Observable @MainActor final class WorkshopModel {
    private let capacity = 6
    private(set) var booked = 0
    private(set) var notice = "No request yet"
    var free: Int { capacity - booked }
    func request(_ amount: Int) {
        guard amount > 0, amount <= free else { notice = "Request refused; state unchanged"; return }
        booked += amount
        notice = "Accepted by this local model only"
    }
}

struct DemoView: View {
    @State private var model = WorkshopModel()
    var body: some View {
        VStack(spacing: 20) {
            Text("Places remaining: \(model.free)").font(.title)
            Button("Request two places") { model.request(2) }
            Text(model.notice)
            Text("In-memory teaching model only.").font(.caption)
        }.padding()
    }
}

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

Expected behaviour and manual checks

Manual check: request two places until the model is full, then confirm refusal leaves the count unchanged. Restart the app to inspect the deliberate lack of durable storage. Not executed here.

The view never assigns booked directly. The model checks the requested transition and changes its notice according to the actual local result. Adding a real service would require an explicit pending, error, ownership and accepted-record contract.

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