Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the framework needed for SwiftData.
iOS Swift
import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing UIKit or SwiftUI instead of SwiftData.
Forgetting to import any framework.
✗ Incorrect
You need to import SwiftData to use the modern persistence features.
2fill in blank
mediumComplete the code to declare a SwiftData model class named 'Note'.
iOS Swift
@Model class [1] { var text: String init(text: String) { self.text = text } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'note' for the class name.
Using plural 'Notes' for a single model.
✗ Incorrect
The model class should be named Note with a capital N to follow Swift naming conventions.
3fill in blank
hardFix the error in the code to create a SwiftData container in the app struct.
iOS Swift
var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(for: [1].self)
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or plural names that don't match the model class.
Forgetting to add .self after the model name.
✗ Incorrect
The model container needs the model type Note.self with the exact class name.
4fill in blank
hardFill both blanks to declare a SwiftData model with an integer id and a string title.
iOS Swift
@Model class Task { var id: [1] var title: [2] init(id: Int, title: String) { self.id = id self.title = title } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using String for id or Int for title.
Using unsupported types like Double or Bool here.
✗ Incorrect
The id should be an Int and the title a String.
5fill in blank
hardFill all three blanks to create a SwiftData model with a UUID id, a string name, and a boolean done flag.
iOS Swift
@Model class Item { var id: [1] = [2]() var name: [3] var done: Bool = false init(name: String) { self.name = name } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Int for id instead of UUID.
Not initializing UUID with UUID().
Using Int for name instead of String.
✗ Incorrect
The id is a UUID initialized with UUID(), and name is a String.