CRUD stands for Create, Read, Update, and Delete. These are the four basic operations to manage data in an app.
Click to reveal answer
beginner
How do you create a new object in SwiftData?
You create a new object by initializing your model and then saving the context. For example, use @Model to define data and then call context.insert(newObject) followed by context.save().
Click to reveal answer
beginner
How do you fetch data using SwiftData?
Use a @Query property wrapper to fetch data. It automatically keeps your UI updated when data changes. For example, @Query var items: [Item] fetches all Item objects.
Click to reveal answer
intermediate
How do you update an existing object in SwiftData?
To update, change the properties of the fetched object and then save the context. SwiftData tracks changes automatically, so just call context.save() after editing.
Click to reveal answer
beginner
What is the correct way to delete an object in SwiftData?
Call context.delete(object) on the object you want to remove, then save the context with context.save(). This removes the object from the database.
Click to reveal answer
Which SwiftData property wrapper is used to fetch data reactively?
A@Query
B@State
C@Published
D@ObservedObject
✗ Incorrect
@Query fetches data from SwiftData and updates the UI automatically when data changes.
What must you do after creating or modifying data in SwiftData to save changes?
ACall context.save()
BRestart the app
CCall context.fetch()
DNothing, changes save automatically
✗ Incorrect
You must call context.save() to persist changes to the database.
How do you delete an object from SwiftData?
ASet object to nil
BRemove the object from an array
Ccontext.delete(object) then context.save()
DCall object.remove()
✗ Incorrect
Deleting requires calling context.delete(object) and then saving the context.
Which CRUD operation involves changing properties of an existing object?
ADelete
BCreate
CRead
DUpdate
✗ Incorrect
Update means modifying existing data.
What is the purpose of the @Model attribute in SwiftData?
AFetches data from the database
BDefines a data model class
CDeletes data
DUpdates UI state
✗ Incorrect
@Model marks a class or struct as a data model for SwiftData.
Explain the steps to create and save a new data object using SwiftData.
Think about how you add a new item to a list and save it.
You got /4 concepts.
Describe how SwiftData helps keep your app UI updated when data changes.
Imagine your app list changes automatically when you add or remove items.
You got /3 concepts.
Practice
(1/5)
1. What does the @Model attribute in SwiftData represent?
easy
A. A UI component for displaying lists
B. A data model class used to define entities for CRUD operations
C. A function to fetch data from the network
D. A SwiftUI view modifier for styling
Solution
Step 1: Understand the role of @Model
The @Model attribute marks a class as a data model entity for SwiftData, enabling CRUD operations.
Step 2: Differentiate from UI and functions
It is not related to UI components, network functions, or view modifiers.
Final Answer:
A data model class used to define entities for CRUD operations -> Option B
Quick Check:
@Model defines data entities = B [OK]
Hint: Remember @Model marks data classes, not UI or functions [OK]
Common Mistakes:
Confusing @Model with UI components
Thinking @Model fetches data
Mixing @Model with view modifiers
2. Which of the following is the correct way to declare a SwiftData model class named Book with a title property of type String?
easy
A. @Model class Book { var title: String }
B. @Model struct Book { var title: String }
C. struct Book { var title: String }
D. class Book: ObservableObject { var title: String }
Solution
Step 1: Identify correct @Model usage
SwiftData requires @Model on a class, not a struct, to define a data entity.
Step 2: Check class vs struct and attribute
@Model class Book { var title: String } correctly uses @Model with a class and a property.
Final Answer:
@Model class Book { var title: String } -> Option A
Quick Check:
@Model + class + properties = D [OK]
Hint: Use @Model with class, not struct, for SwiftData models [OK]
Common Mistakes:
Using struct instead of class for @Model
Omitting @Model attribute
Using ObservableObject instead of @Model
3. Given the following SwiftData code snippet, what will be the output when printing 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)\")
}
}
medium
A. Books count: 0
B. Books count: 2
C. Books count: 1
D. Compilation error
Solution
Step 1: Understand @Query and modelContext.insert
The @Query property fetches all Book entities. Inserting a new Book updates the data source.
Step 2: Check the effect of insertion on books.count
After inserting one new Book, the count increases by 1, so it will be 1.
C. book.title cannot be changed directly; must create new Book
D. modelContext.save() is not a valid method in SwiftData
Solution
Step 1: Check SwiftData update pattern
In SwiftData, changes to model object properties are tracked automatically, but modelContext.save() is not a valid method to persist changes explicitly.
Step 2: Identify if there's an error
Directly mutating book.title is correct, but calling modelContext.save() causes an error because the method does not exist.
Final Answer:
modelContext.save() is not a valid method in SwiftData -> Option D
Quick Check:
Property change tracked automatically; no save() method = A [OK]
Hint: SwiftData tracks changes automatically; no save() method needed [OK]
Common Mistakes:
Thinking modelContext.save() exists
Believing properties are immutable
Confusing update with insert
5. You want to delete all Book objects with an empty title using SwiftData. Which code snippet correctly performs this operation?
hard
A. books.filter { $0.title.isEmpty }.forEach { modelContext.delete($0) }
B. for book in books where book.title.isEmpty { modelContext.delete(book) }
C. modelContext.deleteAll(Book.self, where: \"title == ''\")
D. modelContext.remove(books.filter { $0.title.isEmpty })
Solution
Step 1: Understand deletion in SwiftData
To delete objects, you call modelContext.delete(object) for each object.
Step 2: Check each option for correctness
books.filter { $0.title.isEmpty }.forEach { modelContext.delete($0) } filters books with empty titles and deletes each correctly. for book in books where book.title.isEmpty { modelContext.delete(book) } syntax is invalid in Swift. modelContext.deleteAll(Book.self, where: \"title == ''\") uses a non-existent method. modelContext.remove(books.filter { $0.title.isEmpty }) uses wrong method name.
Final Answer:
books.filter { $0.title.isEmpty }.forEach { modelContext.delete($0) } -> Option A
Quick Check:
Filter then delete each = A [OK]
Hint: Filter then delete each object individually [OK]
Common Mistakes:
Using invalid for-in with where clause
Calling non-existent deleteAll method
Using wrong method name like remove
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.