0
0
iOS Swiftmobile~10 mins

Model definition with @Model in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AModel
BObservableObject
CState
DView
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ObservableObject instead of @Model
Forgetting the '@' symbol
Using @State which is for UI state, not models
2fill in blank
medium

Complete 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'
Aage
Bscore
Cweight
Dheight
Attempts:
3 left
💡 Hint
Common Mistakes
Using a property name that doesn't match the initializer
Using a wrong data type
3fill in blank
hard

Fix 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'
AUnique
BAttribute(.unique)
CAttribute
DID
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@ID' which is not a valid attribute
Forgetting to specify uniqueness
4fill in blank
hard

Fill 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'
AisActive
BisEnabled
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different property name than the initializer
Setting default value to false instead of true
5fill in blank
hard

Fill 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'
AdisplayName
Bname
Cuppercased
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'age' instead of 'name' for uppercase conversion
Misspelling 'uppercased' method
Using wrong computed property name