0
0
iOS Swiftmobile~5 mins

Model definition with @Model in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the @Model attribute do in Swift?
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
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.
How do you declare a property inside an @Model class?
Alet propertyName: Type
Bvar propertyName: Type
C@State var propertyName: Type
Dfunc propertyName() -> Type
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.
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.
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.