0Pricing
Swift Academy · Lesson

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

  1. Core Data Stack: NSPersistentContainer Setup
  2. SwiftData @Model and ModelContext
  3. Relationships and Fetch Descriptors
  4. Lightweight Migrations and CloudKit Sync
← Back to Swift Academy