Bird
0
0

Why does this code cause a compile error? struct Book: Identifiable { var id: Int var title: String } let b = Book(title: "Swift Guide")

medium📝 Debug Q7 of 15
iOS Swift - Lists and Data Display
Why does this code cause a compile error? struct Book: Identifiable { var id: Int var title: String } let b = Book(title: "Swift Guide")
AMissing id parameter in initializer call.
Btitle property must be optional.
CStruct cannot have var properties.
DIdentifiable requires a class, not struct.
Step-by-Step Solution
Solution:
  1. Step 1: Check struct initialization

    Struct has two properties: id and title, both non-optional.
  2. Step 2: Analyze initializer call

    Initializer call provides only title, missing id, causing compile error.
  3. Final Answer:

    Missing id parameter in initializer call. -> Option A
  4. Quick Check:

    All non-optional properties must be initialized = A [OK]
Quick Trick: Initialize all non-optional properties when creating struct [OK]
Common Mistakes:
  • Assuming missing id is allowed
  • Thinking title must be optional
  • Confusing struct and class rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes