Bird
0
0

Identify the error in this model definition:

medium📝 Debug Q14 of 15
iOS Swift - Local Data Persistence
Identify the error in this model definition:
@Model class Product {
  var name: String
  var price: Double
  func discount() -> Double {
    return price * 0.9
  }
}
ANo error, this is valid
BModels cannot have functions inside them
Cprice should be an Int, not Double
DMissing initializer for properties
Step-by-Step Solution
Solution:
  1. Step 1: Check @Model class rules

    @Model classes are data containers that support stored properties, computed properties, initializers (synthesized), and methods.
  2. Step 2: Analyze potential issues

    No syntax errors: initializer synthesized by @Model, Double valid for price, methods allowed.
  3. Final Answer:

    No error, this is valid -> Option A
  4. Quick Check:

    @Model class = supports methods [OK]
Quick Trick: @Model classes support methods [OK]
Common Mistakes:
  • Thinking @Model prohibits functions
  • Believing explicit initializer always required
  • Insisting price must be Int

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes