100 Days of SwiftUI
2026-06-27
Project 11, part 2
Designing good data structures is critically important
Creating books with SwiftData
- create a data model
- make sure to
import SwiftData - use
@Modelfor aclass - the class needs to be initialized
@Model requires an initializer be provided for 'Book' (from macro 'Model') - XCode completion is your friend here
- make sure to
- ask SwiftData to create a model container
- go to the
Appfile and add the modifier.modelContainer(for: Book.self)toWindowGroup - don’t forget to
import SwiftData
- go to the
- make a form that can create new entries
- use
@Environmentproperty to get access to the model context - still need
@Statelocal properties to store the data while working with it
- use


Adding a custom star rating component
- design for flexibility (maybe reuse in a later project?) — which isn’t exactly YAGNI but learning more is better.
- need to use
@Binding - the
#Previewwon’t build unless you give it some data .buttonStyle(.plain)makes it possible to tap individual stars (rather than have a single tap select all 5)
Also, having a library of UI components can’t be all bad
Building a list with @Query
- SwiftData automatically makes items
Identifiableso you don’t need to addid:...to yourForEachloop