Bird
0
0

Identify the error in this SwiftUI code snippet:

medium📝 Debug Q14 of 15
iOS Swift - SwiftUI Layout
Identify the error in this SwiftUI code snippet:
ScrollView {
  LazyVStack {
    ForEach(0..<5) { i in
      Text("Item \(i)")
    }
  }
}
AMissing id parameter in ForEach causes a compile error
BLazyVStack cannot be inside ScrollView
CText view syntax is incorrect
DRange 0..<5 is invalid in ForEach
Step-by-Step Solution
Solution:
  1. Step 1: Check ForEach usage with range

    ForEach with range 0..<5 requires an id parameter to identify each element uniquely.
  2. Step 2: Confirm other parts are correct

    LazyVStack inside ScrollView is valid; Text syntax is correct; range 0..<5 is valid.
  3. Final Answer:

    Missing id parameter in ForEach causes a compile error -> Option A
  4. Quick Check:

    ForEach with range needs id = A [OK]
Quick Trick: Always add id: \.self for ForEach with ranges [OK]
Common Mistakes:
  • Forgetting id parameter in ForEach with ranges
  • Thinking LazyVStack can't be inside ScrollView
  • Misreading range syntax as invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes