Bird
0
0

What will be displayed when this SwiftUI code runs?

medium📝 Predict Output Q13 of 15
iOS Swift - SwiftUI Basics
What will be displayed when this SwiftUI code runs?
struct ContentView: View {
  @State private var count = 0
  var body: some View {
    VStack {
      Text("Count: \(count)")
      Button("Increment") {
        count += 1
      }
    }
  }
}
AA text showing 'Count: 0' and a button labeled 'Increment' that increases count when tapped.
BA static text 'Count: 0' with no button.
CA button labeled 'Increment' but no text showing count.
DThe app crashes due to syntax error.
Step-by-Step Solution
Solution:
  1. Step 1: Understand @State and UI update

    The @State variable 'count' starts at 0 and updates the UI automatically when changed.
  2. Step 2: Analyze the VStack content

    The VStack shows a Text displaying the current count and a Button that increments count on tap, updating the Text.
  3. Final Answer:

    A text showing 'Count: 0' and a button labeled 'Increment' that increases count when tapped. -> Option A
  4. Quick Check:

    @State updates UI automatically = C [OK]
Quick Trick: @State variables update UI automatically on change [OK]
Common Mistakes:
  • Thinking UI won't update after count changes
  • Missing the Button or Text in output
  • Assuming syntax error where none exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes