Bird
0
0

Find the bug in this code snippet:

medium📝 Debug Q7 of 15
iOS Swift - SwiftUI Layout
Find the bug in this code snippet:
LazyHGrid(rows: [GridItem(.flexible())]) {
  ForEach(0..<3) { i in
    Text("Item \(i)")
  }
  .frame(height: 50)
}
ALazyHGrid requires columns parameter, not rows
BGridItem must be .fixed() not .flexible()
CThe .frame modifier is applied to ForEach instead of each Text
DForEach range should be 1..<4 instead of 0..<3
Step-by-Step Solution
Solution:
  1. Step 1: Check modifier placement

    The .frame(height: 50) is applied to the entire ForEach, not individual Text views.
  2. Step 2: Understand effect

    This means the frame affects the container, not each item, which may cause layout issues.
  3. Final Answer:

    The .frame modifier is applied to ForEach instead of each Text -> Option C
  4. Quick Check:

    Modifiers should be on individual views for correct layout [OK]
Quick Trick: Apply modifiers to individual views, not ForEach container [OK]
Common Mistakes:
  • Applying modifiers to ForEach instead of children
  • Confusing rows and columns in LazyHGrid
  • Incorrect ForEach ranges

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes