Bird
0
0

What will happen when this SwiftUI code runs? struct ContentView: View { @State private var count = 0 var body: some View { VStack { Button("Tap me") { count += 1 } Text("Count: \(count)") } } }

medium📝 Predict Output Q5 of 15
iOS Swift - SwiftUI Basics
What will happen when this SwiftUI code runs? struct ContentView: View { @State private var count = 0 var body: some View { VStack { Button("Tap me") { count += 1 } Text("Count: \(count)") } } }
AButton increments count but Text does not update
BButton does nothing and Text shows Count: 0 always
CApp crashes due to missing VStack or container
DButton increments count and Text updates to show new count
Step-by-Step Solution
Solution:
  1. Step 1: Understand @State usage

    @State allows the view to update when the variable changes, so count updates trigger UI refresh.
  2. Step 2: Check Button and Text behavior

    Button increments count, and Text shows the updated count dynamically.
  3. Final Answer:

    Button increments count and Text updates to show new count -> Option D
  4. Quick Check:

    @State triggers UI update = A [OK]
Quick Trick: @State variables update UI when changed in SwiftUI [OK]
Common Mistakes:
  • Forgetting @State causes no UI update
  • Assuming missing container causes crash
  • Thinking Text won't update automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes