Featured blog image
IOS 10 min read

Day 2/30 Getting Started with SwiftData in SwiftUI

Author

Arjun

IOS Developer

💡 What is SwiftData?

SwiftData is Apple’s modern persistence framework introduced in iOS 17.
It is like Core Data but redesigned for SwiftUI — lightweight, Swift-native, and declarative.

Here’s what makes it nice:

  • ✅ Write plain Swift classes marked with @Model

  • ✅ No need to create Core Data entities manually

  • ✅ Automatic persistence to disk

  • ✅ Tight integration with SwiftUI using @Query and .modelContainer()

With SwiftData, storing and reading data becomes almost as easy as working with arrays.

🏗 Step 1 — Creating a Model

We first define the data type we want to save.
In my app, I wanted to store “wishes” — things I want to achieve or buy.

@Model
class Wish {
   public var name: String
   init(name: String) {
       self.name = name
   }
}

That’s it — just a Swift class with @Model and SwiftData does the rest:
It automatically creates the database table, IDs, and relationships behind the scenes.

🧠 Step 2 — Setting up Model Container

We need a ModelContainer to store the data.
This container is like the “database” of our app.

@main
struct SwiftDataApp: App {
   var sharedModelContainer: ModelContainer = {
       let schema = Schema([ Wish.self ])
       let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)

       do {
           return try ModelContainer(for: schema, configurations: modelConfiguration)
       } catch {
           fatalError("Could not create ModelContainer: \(error)")
       }
   }()
   
   var body: some Scene {
       WindowGroup {
           SwiftDataView()
               .modelContainer(sharedModelContainer)
       }
   }
}
 

Schema tells SwiftData which models exist

ModelConfiguration sets where data is stored

modelContainer() injects it into the SwiftUI environment

🖥 Step 3 — Querying Data in SwiftUI

SwiftData gives us a @Query property wrapper to fetch data directly into a view.
It automatically watches for changes and updates the UI.

import SwiftUI
import SwiftData

struct SwiftDataView: View {
  @Environment(\.modelContext) private var viewContext
  @Query var items: [Wish]
   
   var body: some View {
       NavigationStack {
           List {
               ForEach(items) { item in
                   Text(item.name)
                       .font(.subheadline)
                       .padding(.vertical, 8)
               }
           }
           .navigationTitle("Wishes")
           .overlay {
               if items.isEmpty {
                   ContentUnavailableView("No wish add Yet!", systemImage: "heart.fill")
               }
           }
       }
   }
}

👀 Preview with Empty View

#Preview("Empty Data View") {

    SwiftDataView().modelContainer(for: Wish.self, inMemory: true)
}

👀 Preview with Sample Data

#Preview("Data View with Sample Data") {
   let modelContainer = try! ModelContainer(
       for: Wish.self,
       configurations: ModelConfiguration(isStoredInMemoryOnly: true)
   )
   modelContainer.mainContext.insert(Wish(name: "Purchase iPhone"))
   modelContainer.mainContext.insert(Wish(name: "Learn SwiftUI"))
   modelContainer.mainContext.insert(Wish(name: "Build a Mobile App"))
   
   return SwiftDataView()
       .modelContainer(modelContainer)
}

This lets me quickly see how my app looks with mock data.


✅ Wrap-Up

With just a few lines, I created a persistent list of wishes using SwiftData.
Tomorrow, I’ll explore how to add and delete items from the list.

SwiftData is making data storage much easier in SwiftUI — I’m already liking it. 💖

Related Topics

Arjun

IOS Developer
Passionate iOS Developer with experience building intuitive and high-performance apps using Swift and SwiftUI. Skilled in designing clean architectures, integrating APIs, and optimizing user experience. Constantly exploring new Apple technologies to deliver impactful mobile solutions.