SwiftData Basics

SwiftData (iOS 17+) is Apple's modern, code-first persistence framework. You'll define models with @Model , set up a ModelContainer , fetch with @Query , insert and delete objects, and see how it improves on Core Data.

Learn SwiftData Basics in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Define a Model with @Model

A SwiftData model is just a class with the @Model macro. The macro adds persistence and change tracking — no separate model file, no setValue(forKey:) .

2️⃣ Set Up the Container

Attach a ModelContainer once at the app root with .modelContainer(for:) . Views then read the active ModelContext from the environment.

Your turn. Fill in the macro and the model type.

3️⃣ Query, Insert, and Delete

@Query fetches models with type-safe sorting and keeps the view in sync. Use context.insert(_:) to add and context.delete(_:) to remove.

Now you try. Fill in the insert and delete methods.

📋 Quick Reference

Build a small @Model-backed to-do list with @Query, insert, and delete — and notice how little code it takes.

Practice quiz

Which macro marks a class as a SwiftData model?

  • @State
  • @Model
  • @Entity
  • @Persisted

Answer: @Model. @Model turns a class into a persistent SwiftData model.

From which iOS version is SwiftData available?

  • iOS 13
  • iOS 15
  • iOS 17
  • iOS 11

Answer: iOS 17. SwiftData was introduced with iOS 17.

What sets up SwiftData's storage for your app?

  • NSPersistentContainer
  • ModelContainer
  • URLSession
  • ModelView

Answer: ModelContainer. A ModelContainer configures and holds SwiftData's storage.

Which property wrapper fetches and observes model objects in a view?

  • @FetchRequest
  • @Query
  • @Published
  • @Binding

Answer: @FetchRequest. @Query loads SwiftData models and keeps the view updated.

Which context method adds a new model object?

  • context.insert(object)
  • context.add(object)
  • context.append(object)
  • context.push(object)

Answer: context.insert(object). modelContext.insert(_:) inserts a new object into the store.

Which context method removes a model object?

  • context.remove(object)
  • context.discard(object)
  • context.drop(object)
  • context.delete(object)

Answer: context.delete(object). modelContext.delete(_:) removes the object.

Compared with Core Data, SwiftData models are defined using...

  • An .xcdatamodeld editor
  • Plain Swift classes with @Model
  • JSON files
  • Property lists

Answer: Plain Swift classes with @Model. SwiftData uses code-first @Model classes instead of a visual model editor.

How do you reach the model context inside a SwiftUI view?

  • @State var context
  • @Environment(\.modelContext)
  • URLSession.shared
  • @Binding context

Answer: @Environment(\.modelContext). @Environment(\.modelContext) provides the active context to the view.

What is SwiftData built on top of?

  • Realm
  • Core Data's proven engine
  • SQLite only, no framework
  • UserDefaults

Answer: Core Data's proven engine. SwiftData is a modern Swift API layered over Core Data's engine.

A key advantage of SwiftData over Core Data is...

  • It needs Objective-C
  • It cannot persist data
  • It only works on macOS
  • A type-safe, code-first Swift API with less boilerplate

Answer: A type-safe, code-first Swift API with less boilerplate. SwiftData offers a cleaner, type-safe Swift-native API with far less boilerplate.