Introduction
CRUD means Create, Read, Update, and Delete data. SwiftData helps you do these actions easily in your iOS apps.
CRUD means Create, Read, Update, and Delete data. SwiftData helps you do these actions easily in your iOS apps.
import SwiftData // Define a model @Model class Item { var id: UUID var name: String init(id: UUID = UUID(), name: String) { self.id = id self.name = name } } // Create let newItem = Item(name: "Example") modelContext.insert(newItem) // Read let items = try modelContext.fetch(Item.self) // Update newItem.name = "Updated Name" try modelContext.save() // Delete modelContext.delete(newItem) try modelContext.save()
@Model
class Task {
var id: UUID
var title: String
var done: Bool
init(id: UUID = UUID(), title: String, done: Bool = false) {
self.id = id
self.title = title
self.done = done
}
}let task = Task(title: "Buy milk") modelContext.insert(task) try modelContext.save()
let tasks = try modelContext.fetch(Task.self) for task in tasks { print(task.title) }
task.done = true
try modelContext.save()This app lets you add notes, see them in a list, and delete them by swiping. It uses SwiftData to save notes automatically.
import SwiftUI import SwiftData @Model class Note { var id: UUID var content: String init(id: UUID = UUID(), content: String) { self.id = id self.content = content } } struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var notes: [Note] @State private var newNote = "" var body: some View { VStack { TextField("New note", text: $newNote) .textFieldStyle(.roundedBorder) .padding() Button("Add Note") { let note = Note(content: newNote) modelContext.insert(note) try? modelContext.save() newNote = "" } .padding() List { ForEach(notes, id: \.id) { note in Text(note.content) } .onDelete { indexSet in for index in indexSet { let note = notes[index] modelContext.delete(note) } try? modelContext.save() } } } .padding() } } @main struct NotesApp: App { var body: some Scene { WindowGroup { ContentView() .modelContainer(for: [Note.self]) } } }
try? modelContext.save() after insert, update, or delete.@Query to automatically fetch and update your UI when data changes.try? for simplicity.@Model classes and modelContext to manage data.@Query to fetch data and keep your UI updated automatically.@Model attribute in SwiftData represent?@Model attribute marks a class as a data model entity for SwiftData, enabling CRUD operations.Book with a title property of type String?@Model on a class, not a struct, to define a data entity.@Model with a class and a property.books.count after adding a new book?
struct ContentView: View {
@Environment(\"modelContext\") private var modelContext
@Query var books: [Book]
var body: some View {
Button(\"Add Book\") {
let newBook = Book(title: \"SwiftData Basics\")
modelContext.insert(newBook)
}
Text(\"Books count: \(books.count)\")
}
}@Query property fetches all Book entities. Inserting a new Book updates the data source.func updateBookTitle(book: Book, newTitle: String) {
book.title = newTitle
modelContext.save()
}modelContext.save() is not a valid method to persist changes explicitly.book.title is correct, but calling modelContext.save() causes an error because the method does not exist.Book objects with an empty title using SwiftData. Which code snippet correctly performs this operation?modelContext.delete(object) for each object.Pass the test and this pattern moves unknown