Bird
0
0

What is the issue with this SwiftUI code snippet?

medium📝 Debug Q6 of 15
iOS Swift - SwiftUI Basics
What is the issue with this SwiftUI code snippet?
struct ContentView: View {
  var body: some View {
    Text("Hello")
    Text("World")
  }
}
AThe body property should be a function, not a computed property.
BText views cannot be used inside the body property.
CThe struct is missing an @State property wrapper.
DThe body must return a single View, but here it returns two Text views without a container.
Step-by-Step Solution
Solution:
  1. Step 1: Understand SwiftUI body requirements

    The body property must return exactly one View.
  2. Step 2: Analyze the code

    Two Text views are returned side-by-side without a container like VStack.
  3. Step 3: Identify the error

    Returning multiple sibling views without a container causes a compile error.
  4. Final Answer:

    The body must return a single View, but here it returns two Text views without a container. -> Option D
  5. Quick Check:

    Body returns one View only [OK]
Quick Trick: Body must return one View, wrap multiple views in containers [OK]
Common Mistakes:
  • Returning multiple sibling views without container
  • Confusing @State necessity for this code
  • Misunderstanding body as a function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes