0
0
iOS Swiftmobile~20 mins

Model definition with @Model in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of @Model
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding @Model property wrapper usage
What is the primary purpose of using the @Model property wrapper in SwiftUI apps?
ATo automatically manage the lifecycle and persistence of data models in the app.
BTo create a user interface element that displays model data.
CTo define a function that updates the UI when data changes.
DTo style views with custom colors and fonts.
Attempts:
2 left
💡 Hint
Think about how data is stored and updated in SwiftUI apps.
ui_behavior
intermediate
1:30remaining
Effect of @Model on UI updates
Given a SwiftUI view with a @Model property, what happens when a property inside the model changes?
AThe view ignores the change until manually refreshed.
BThe view automatically refreshes to reflect the updated data.
CThe app crashes due to data inconsistency.
DThe model resets to its initial state.
Attempts:
2 left
💡 Hint
Consider how SwiftUI reacts to changes in observed data.
📝 Syntax
advanced
2:00remaining
Correct syntax for defining a @Model struct
Which of the following code snippets correctly defines a Swift data model using @Model?
iOS Swift
Choose the correct code snippet:
A
import SwiftUI
struct Task {
  @Model var title: String
  @Model var isDone: Bool
}
B
import SwiftUI
@Model
class Task {
  var title: String
  var isDone: Bool
}
C
import SwiftUI
@Model
struct Task {
  var title: String
  var isDone: Bool
}
D
import SwiftUI
@Model
struct Task {
  let title: String
  let isDone: Bool
}
Attempts:
2 left
💡 Hint
Remember @Model is applied to the whole struct, not individual properties.
lifecycle
advanced
2:00remaining
Persistence behavior of @Model data
When you mark a struct with @Model, what happens to instances of this model when the app is closed and reopened?
AThe app throws an error on launch due to missing data.
BThe data is lost and resets to default values.
CThe data is saved only if manually written to a file.
DThe data is automatically saved and restored across app launches.
Attempts:
2 left
💡 Hint
Think about what automatic persistence means for user data.
🔧 Debug
expert
2:30remaining
Identifying error with @Model usage
What error will occur if you define a @Model struct with a property marked as let instead of var?
iOS Swift
import SwiftUI
@Model
struct Note {
  let content: String
}
ACompilation error: Properties in a @Model struct must be mutable (var).
BNo error; let properties are allowed in @Model structs.
CRuntime error: Cannot modify immutable property.
DWarning: Property will not be saved persistently.
Attempts:
2 left
💡 Hint
Consider how @Model tracks changes to properties.