The @Model attribute marks a Swift class as a data model that can be observed for changes and persisted automatically.
Click to reveal answer
beginner
How do you define a simple model with @Model in Swift?
You create a class with the @Model attribute and define properties inside it. For example: <br><code>@Model class User { var id = UUID(); var name: String? }</code>
Click to reveal answer
intermediate
Why use @Model instead of a regular class for data in Swift apps?
Because @Model classes automatically support change tracking and persistence, making it easier to keep UI and data in sync.
Click to reveal answer
intermediate
Can @Model classes have relationships to other @Model classes?
Yes, @Model classes can reference other @Model classes to represent relationships like parent-child or one-to-many.
Click to reveal answer
beginner
What happens when you change a property inside an @Model class instance?
The system automatically detects the change and updates any UI or storage that depends on that model instance.
Click to reveal answer
What keyword do you use to define a data model class that supports automatic persistence in Swift?
Aclass
B@State
C@Model
D@Published
✗ Incorrect
@Model marks a class for automatic persistence and observation.
Which of these is true about @Model classes?
AThey automatically track changes to their properties.
BThey cannot have properties.
CThey are only used for UI layout.
DThey must be structs.
✗ Incorrect
@Model classes automatically track changes to their properties for UI and storage updates.
How do you declare a property inside an @Model class?
Alet propertyName: Type
Bvar propertyName: Type
C@State var propertyName: Type
Dfunc propertyName() -> Type
✗ Incorrect
Properties inside @Model classes are declared as variables with var.
Can @Model classes have relationships to other @Model classes?
AYes, they can reference other @Model classes.
BOnly if they are structs.
CNo, they must be independent.
DOnly if marked with @Published.
✗ Incorrect
@Model classes can reference other @Model classes to model relationships.
What happens when you update a property in an @Model class instance?
ANothing happens automatically.
BThe app crashes.
CYou must manually notify the UI.
DThe UI and storage update automatically.
✗ Incorrect
Changes to @Model properties automatically update UI and storage.
Explain how to define a simple data model using @Model in Swift and why it is useful.
Think about how @Model helps keep UI and data in sync.
You got /5 concepts.
Describe what happens behind the scenes when you change a property in an @Model class instance.
Consider how the app knows to refresh the screen or save data.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using @Model in SwiftUI?
easy
A. To mark a class as a data container for app state
B. To create a user interface element
C. To define a function for navigation
D. To style text in the app
Solution
Step 1: Understand the role of @Model
@Model is used to mark a class that holds data for the app, making it a data container.
Step 2: Differentiate from UI or styling
@Model does not create UI elements or style text; it only defines data structures.
Final Answer:
To mark a class as a data container for app state -> Option A
Quick Check:
@Model = data container [OK]
Hint: Remember: @Model is for data, not UI or styling [OK]
Common Mistakes:
Thinking @Model creates UI components
Confusing @Model with styling attributes
Assuming @Model defines functions
2. Which of the following is the correct syntax to define a model class with @Model in Swift?
easy
A. class User { @Model var name: String }
B. class @Model User { var name: String }
C. class User @Model { var name: String }
D. @Model class User { var name: String }
Solution
Step 1: Recall correct @Model placement
@Model is placed before the class keyword to mark the entire class as a model.
Step 2: Check other options for syntax errors
Other placements of @Model cause syntax errors.
Final Answer:
@Model class User { var name: String } -> Option D
Quick Check:
@Model before class = correct syntax [OK]
Hint: Put @Model right before class keyword [OK]
Common Mistakes:
Placing @Model after class name
Adding @Model before property instead of class
Using @Model inside class body
3. Given the model definition:
@Model class Task { var title: String; var done: Bool }
What is the output of this code?
let task = Task(title: "Buy milk", done: false)
print(task.done)
medium
A. true
B. false
C. nil
D. Compilation error
Solution
Step 1: Understand model initialization
The Task model is initialized with done set to false (@Model synthesizes the initializer).
Step 2: Print the done property value
Printing task.done outputs the Boolean value false.
Final Answer:
false -> Option B
Quick Check:
task.done = false [OK]
Hint: Check property values passed during initialization [OK]
Common Mistakes:
Assuming default true value for done
Expecting nil instead of false
Thinking @Model changes property defaults
4. Identify the error in this model definition:
@Model class Product {
var name: String
var price: Double
func discount() -> Double {
return price * 0.9
}
}
medium
A. No error, this is valid
B. Models cannot have functions inside them
C. price should be an Int, not Double
D. Missing initializer for properties
Solution
Step 1: Check @Model class rules
@Model classes are data containers that support stored properties, computed properties, initializers (synthesized), and methods.
Step 2: Analyze potential issues
No syntax errors: initializer synthesized by @Model, Double valid for price, methods allowed.
Final Answer:
No error, this is valid -> Option A
Quick Check:
@Model class = supports methods [OK]
Hint: @Model classes support methods [OK]
Common Mistakes:
Thinking @Model prohibits functions
Believing explicit initializer always required
Insisting price must be Int
5. You want to create a model to store user profile data with a name and optional age. Which is the best way to define it using @Model?
hard
A. @Model class UserProfile { var name: String; var age: Optional<Int> }
B. @Model class UserProfile { var name: String; var age: Int = nil }
C. @Model class UserProfile { var name: String; var age: Int? }
D. @Model class UserProfile { var name: String; var age: Int! }
Solution
Step 1: Understand optional property syntax
In Swift, optional properties are declared with a question mark, e.g., Int?.
Step 2: Evaluate each option's correctness
@Model class UserProfile { var name: String; var age: Int? } uses correct, idiomatic optional syntax. @Model class UserProfile { var name: String; var age: Int = nil } assigns nil to a non-optional Int, which is invalid. @Model class UserProfile { var name: String; var age: Optional<Int> } uses verbose Optional<Int>. @Model class UserProfile { var name: String; var age: Int! } uses implicitly unwrapped optional which is less safe.
Final Answer:
@Model class UserProfile { var name: String; var age: Int? } -> Option C
Quick Check:
Optional property = Int? [OK]
Hint: Use ? for optional properties in models [OK]
Common Mistakes:
Assigning nil to non-optional types
Using wrong optional syntax
Using implicitly unwrapped optionals unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.