100 Days of SwiftUI

Day 57

2026-06-30

Project 12, part 1

More with SwiftData

SwiftData: Introduction

SwiftData really pushes hard on advanced features of both Swift and SwiftUI, all to help make it easy for us to store data efficiently. It’s not always easy, though, and there are a few places that take quite a bit of thinking to use properly

Editing SwiftData model objects


Snoopy happily jumping. Yay in macrotext Editing Data!!!
  • make the class for the data
  • add to the App with .modelContainer on the WindowGroup
  • add the EditView
    • Remember kids, @Binding != @Bindable
    • update the #Preview with the model example data
  • now make ContentView show the data
samoyed sketchsamoyed sketch

It’s interesting that you don’t need a separate AddView — basically you “edit” an empty default when you want to add new data. I don’t know how I feel about that. It seems incomplete, but also, since it would basically be the same form why would you make two of them?

The way his example goes, you Add with a button, but edit with a link. Hmmm…

Filtering @Query using #Predicate

Grammar class?!
  • empty the data: try? modelContext.delete(model: User.self)

#Predicate is a macro that will check for the parameters in the @Query we want for filtering the data before it’s presented rather than filtering it afterwards.

For some reason my Canvas Preview kept crashing?

Anyway, you can make complicated filters if you can get them to be one expression.