Bird
0
0

Why does this SwiftUI code cause a compile error?

medium📝 Debug Q7 of 15
iOS Swift - SwiftUI Basics
Why does this SwiftUI code cause a compile error?
HStack {
  Text("A")
  Spacer()
  Text("B").padding(10, .leading)
}
APadding parameters are in wrong order; should be padding(.leading, 10)
BSpacer cannot be used inside HStack
CText views cannot have padding
DHStack requires all children to have same modifiers
Step-by-Step Solution
Solution:
  1. Step 1: Check padding syntax

    Correct syntax is padding(edge, length), e.g., padding(.leading, 10).
  2. Step 2: Identify error cause

    Parameters reversed cause compile error.
  3. Final Answer:

    Padding parameters are in wrong order; should be padding(.leading, 10) -> Option A
  4. Quick Check:

    Padding syntax order matters [OK]
Quick Trick: Use padding(.edge, value), not padding(value, .edge) [OK]
Common Mistakes:
  • Swapping padding parameters
  • Misusing Spacer in HStack
  • Assuming all children need same modifiers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes