Swift Data Complete Guide: ORM for Swift with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Swift Data: Apple's New ORM for Swift

1. What It Is and Why You Need It

Swift Data is a modern, lightweight ORM (Object-Relational Mapping) framework introduced by Apple at WWDC 2023. It's designed for persistent data management in Swift apps targeting iOS 17+, macOS 14+, tvOS 17+, and watchOS 10+. Swift Data replaces the aging Core Data framework, offering a simpler, declarative, and type-safe approach to database management.

Why Swift Data? It dramatically simplifies your code. Instead of manually creating NSManagedObject subclasses, configuring contexts, and writing complex fetch requests, you define your data model using regular Swift classes and macros (@Model, @Attribute, @Relationship). Swift Data automatically handles all the behind-the-scenes logic: table creation, change tracking, dependency management, and seamless UI synchronization with SwiftUI.

The key advantage is deep SwiftUI integration. Data changes automatically update the interface without manually calling objectWillChange.send() or reloading lists. This makes Swift Data the ideal choice for new SwiftUI projects requiring local data storage (to-do lists, notes, settings, cache).

2. Setup

Swift Data is part of Apple's standard library starting with iOS 17 and macOS 14. No separate installation via package managers (pip, npm, cargo) is required. Simply import the framework in your Swift file.

Important: Your project's minimum deployment target must be iOS 17.0 or macOS 14.0.

To add it to your project (via Xcode):

// Swift Data requires no installation. Just import:
import SwiftData

If you use Swift Package Manager (SPM) for dependencies, Swift Data doesn't need to be added as a separate package — it's built into the SDK.

3. Quick Start — Minimal Working Example

Let's build a simple to-do list app.

import SwiftUI
import SwiftData

// 1. Define the data model
@Model
final class TaskItem {
    var title: String
    var isCompleted: Bool
    var createdAt: Date

    init(title: String, isCompleted: Bool = false) {
        self.title = title
        self.isCompleted = isCompleted
        self.createdAt = Date()
    }
}

// 2. Configure the container in App
@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(for: TaskItem.self) // Automatically creates the container
    }
}

// 3. Use data in your View
struct ContentView: View {
    @Environment(\.modelContext) private var context
    @Query private var tasks: [TaskItem]

    var body: some View {
        NavigationStack {
            List {
                ForEach(tasks) { task in
                    HStack {
                        Text(task.title)
                        Spacer()
                        if task.isCompleted {
                            Image(systemName: "checkmark")
                        }
                    }
                }
                .onDelete(perform: deleteTasks)
            }
            .navigationTitle("Tasks")
            .toolbar {
                ToolbarItem {
                    Button("Add") {
                        let newTask = TaskItem(title: "New Task \(tasks.count + 1)")
                        context.in                    

Recommendations