Bird
0
0

What will be the output when this SwiftUI code runs?

medium📝 Predict Output Q4 of 15
iOS Swift - User Input and Forms
What will be the output when this SwiftUI code runs?
struct ContentView: View {
  @State private var selected = 1
  var body: some View {
    Picker("Number", selection: $selected) {
      Text("One").tag(1)
      Text("Two").tag(2)
    }
    Text("Selected: \(selected)")
  }
}
AA picker with options One and Two, but no text displayed
BA picker with options One and Two, and text showing Selected: 0 initially
CA picker with options One and Two, and text showing Selected: 1 initially
DCompilation error due to missing tags
Step-by-Step Solution
Solution:
  1. Step 1: Understand initial state value

    The @State variable selected is initialized to 1.
  2. Step 2: Analyze Picker tags and displayed text

    Picker options have tags 1 and 2; text shows current selected value.
  3. Final Answer:

    Picker with options One and Two, text shows Selected: 1 initially -> Option C
  4. Quick Check:

    Picker selection matches initial state [OK]
Quick Trick: Picker shows initial tag value from bound state [OK]
Common Mistakes:
  • Assuming default 0 without tag
  • Forgetting to add tags
  • Expecting no text output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes