100 Days of SwiftUI

Day 53

2026-06-26

Project 11, part 1

Bookworm: Book tracking app — SwiftData! Custom UI Component!

Creating a custom component with @Binding

  • @Binding is not the same as @Bindable
  • @Bindable is for a shared class using the @Observable macro
  • @Binding is for a simple value-type piece of data rather than a separate class

He goes through an example that creates a separate button view that toggles a boolean and then some text in the ContentView reads that and makes a change.

as far as the button is concerned it’s just toggling a Boolean — it has no idea that something else is monitoring that Boolean and acting upon changes

Accepting multi-line text input with TextEditor

  • for longer text input than TextField
  • can bind to @AppStorage but beware that it’s not secure
  • in some situations you can use modifiers on TextField (especially the axis: .vertical) to let the thing grow vertically
  • try it and see!

Form

var body: some View {
    Form {
        Section("Short") {
            TextField("Enter your text", text: $short, axis: .vertical)
                .textFieldStyle(.roundedBorder)
                .padding()
        }
        Section("Long") {
            TextEditor(text: $long)
                .padding()
        }
    }
}

Text in Form

Stack

var body: some View {
        NavigationStack {
            Section("Short") {
                TextField("Enter your text", text: $short, axis: .vertical)
                    .textFieldStyle(.roundedBorder)
                    .padding()
            }
            Section("Long") {
                TextEditor(text: $long)
                    .padding()
            }
        }
    }

Text in Stack

Introduction to SwiftData and SwiftUI

  • more advanced than UserDefaults
  • can do iCloud syncing, etc.
  • takes 3 steps to implement
    1. define the data we want to use in the app
    2. write some code to load the data (tells SwiftData to prepare some storage)
    3. create the model context, which is the live data in memory

OK. I see that I will need to revisit this. It seems simple enough — just adding a few lines here and there — but we all know that “seems simple” is usually more complicated than you ever expected.