Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a model named User using @Model.
iOS Swift
import SwiftData @[1] class User { var name: String init(name: String) { self.name = name } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ObservableObject instead of @Model
Forgetting the '@' symbol
Using @State which is for UI state, not models
✗ Incorrect
The @Model attribute is used to define a data model class in SwiftData.
2fill in blank
mediumComplete the code to add an integer property 'age' to the User model.
iOS Swift
@Model class User { var name: String var [1]: Int init(name: String, age: Int) { self.name = name self.age = age } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a property name that doesn't match the initializer
Using a wrong data type
✗ Incorrect
The property 'age' is added as an Int to the User model.
3fill in blank
hardFix the error in the model by completing the missing keyword to make the 'id' property unique.
iOS Swift
@Model class User { @[1] var id: UUID = UUID() var name: String init(name: String) { self.name = name } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@ID' which is not a valid attribute
Forgetting to specify uniqueness
✗ Incorrect
The '@Attribute(.unique)' marks the 'id' property as unique in the model.
4fill in blank
hardFill both blanks to define a model with a default value for 'isActive' property.
iOS Swift
@Model class User { var name: String var [1]: Bool = [2] init(name: String, isActive: Bool = true) { self.name = name self.isActive = isActive } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different property name than the initializer
Setting default value to false instead of true
✗ Incorrect
The property 'isActive' is a Bool with a default value true.
5fill in blank
hardFill all three blanks to define a model with a computed property 'displayName' that returns the uppercase name.
iOS Swift
@Model class User { var name: String var age: Int var [1]: String { return [2].[3]() } init(name: String, age: Int) { self.name = name self.age = age } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'age' instead of 'name' for uppercase conversion
Misspelling 'uppercased' method
Using wrong computed property name
✗ Incorrect
The computed property 'displayName' returns the uppercase version of 'name' using 'uppercased()'.