-
Build an app with SwiftData
Discover how SwiftData can help you persist data in your app. Code along with us as we bring SwiftData to a multi-platform SwiftUI app. Learn how to convert existing model classes into SwiftData models, set up the environment, reflect model layer changes in UI, and build document-based applications backed by SwiftData storage.
To get the most out of this session, you should be familiar SwiftData. For an introduction, check out "Meet SwiftData" from WWDC23.Capítulos
- 0:00 - Meet the app
- 3:09 - SwiftData models
- 5:30 - Querying models to display in UI
- 9:43 - Creating and updating
- 12:30 - Document-based apps
Recursos
Vídeos relacionados
WWDC23
-
Buscar neste vídeo...
-
-
3:33 - Defining a SwiftData model
@Model final class Card { var front: String var back: String var creationDate: Date init(front: String, back: String, creationDate: Date = .now) { self.front = front self.back = back self.creationDate = creationDate } } -
4:25 - Binding to a SwiftData model
@Bindable var card: Card -
5:52 - Query models from SwiftData storage
@Query private var cards: [Card] -
8:27 - Setting up a model container for the window group
WindowGroup { ContentView() } .modelContainer(for: Card.self) -
9:24 - Providing a preview with sample data
#Preview { ContentView() .frame(minWidth: 500, minHeight: 500) .modelContainer(previewContainer) } -
10:30 - Accessing the model context of the ContentView
@Environment(\.modelContext) private var modelContext -
10:51 - Insert a new model in the context
let newCard = Card(front: "Sample Front", back: "Sample Back") modelContext.insert(object: newCard) -
13:34 - Start document-based application setup
@main struct SwiftDataFlashCardSample: App { var body: some Scene { #if os(iOS) || os(macOS) DocumentGroup(editing: Card.self, contentType: <#UTType#>) { <#code#> } #else WindowGroup { ContentView() .modelContainer(for: Card.self) } #endif } } -
16:51 - Finish document-based application setup
@main struct SwiftDataFlashCardSample: App { var body: some Scene { #if os(iOS) || os(macOS) DocumentGroup(editing: Card.self, contentType: .flashCards) { ContentView() } #else WindowGroup { ContentView() .modelContainer(for: Card.self) } #endif } }
-