Bird
0
0

Identify the error in this SwiftUI code snippet:

medium📝 Debug Q14 of 15
iOS Swift - iOS Basics and Setup
Identify the error in this SwiftUI code snippet:
struct ContentView: View {
  var body: some View {
    Button("Click me") {
      print("Clicked")
    }
  }
}
AThe body property must return some View explicitly.
BNo syntax error; the code is correct.
CThe body property must be marked with <code>@ViewBuilder</code>.
DThe Button label should be a closure, not a string.
Step-by-Step Solution
Solution:
  1. Step 1: Check body property syntax

    In SwiftUI, the body computed property implicitly returns the view using @ViewBuilder.
  2. Step 2: Verify the Button syntax

    Button("Click me") { print("Clicked") } is valid; no explicit return needed even for multi-line expressions.
  3. Final Answer:

    No syntax error; the code is correct. -> Option B
  4. Quick Check:

    SwiftUI body implicitly returns views [OK]
Quick Trick: SwiftUI body uses implicit return via ViewBuilder [OK]
Common Mistakes:
  • Thinking an explicit return is required
  • Believing @ViewBuilder must be marked explicitly
  • Assuming Button requires closure label only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes