SwiftData @Model and ModelContext
Defining models with @Model macro and performing CRUD via ModelContext.
SwiftData Introduction
SwiftData (iOS 17+) is Apple's modern, Swift-native persistence framework built on Core Data under the hood.
import SwiftData
// Main concepts: @Model, ModelContainer, ModelContext@Model Macro
Annotate a class with @Model to make it a persistable SwiftData model. Properties become stored attributes automatically.
@Model
class Item {
var name: String
var createdAt: Date
init(name: String) {
self.name = name
self.createdAt = Date()
}
}All lessons in this course
- Core Data Stack: NSPersistentContainer Setup
- SwiftData @Model and ModelContext
- Relationships and Fetch Descriptors
- Lightweight Migrations and CloudKit Sync