Hint: Picker needs selection binding with $ and label closure [OK]
Common Mistakes:
Omitting $ for binding
Wrong parameter order
Using non-closure label
3. Given this SwiftUI code snippet, what will be displayed when the user selects "Banana"?
struct ContentView: View {
@State private var fruit = "Apple"
var fruits = ["Apple", "Banana", "Cherry"]
var body: some View {
VStack {
Picker("Select Fruit", selection: $fruit) {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}
}
Text("You chose: \(fruit)")
}
}
}
medium
A. You chose: Banana
B. You chose: "" (empty string)
C. You chose: Cherry
D. You chose: Apple
Solution
Step 1: Understand state binding in Picker
The Picker binds to @State variable 'fruit', updating it when user selects an option.
Step 2: User selects "Banana"
The state variable 'fruit' changes to "Banana", so the Text below shows "You chose: Banana".
Final Answer:
You chose: Banana -> Option A
Quick Check:
Picker selection updates state = displayed text [OK]
Hint: Picker updates bound state, Text shows updated value [OK]
Common Mistakes:
Ignoring state update on selection
Confusing initial value with selected value
Not using id: \.self in ForEach
4. Identify the error in this DatePicker code snippet:
@State private var selectedDate = Date()
var body: some View {
DatePicker("Pick a date", selection: selectedDate, displayedComponents: .date)
}
medium
A. DatePicker cannot display only date component
B. selectedDate must be a String, not Date
C. Label parameter must be omitted
D. Missing $ before selectedDate in selection binding
Solution
Step 1: Check binding syntax for DatePicker
The selection parameter requires a binding, so it must be $selectedDate, not selectedDate.
Step 2: Verify other parameters
Displaying only date is valid, label is required, and selectedDate is correctly a Date type.
Final Answer:
Missing $ before selectedDate in selection binding -> Option D
Quick Check:
Binding needs $ prefix [OK]
Hint: Always use $ for binding state variables [OK]
Common Mistakes:
Forgetting $ in binding
Using wrong data type for selection
Misunderstanding displayedComponents usage
5. You want to create a SwiftUI view where users pick a time and a fruit from a list. Which code snippet correctly combines a DatePicker for time and a Picker for fruits with state variables?
hard
A. struct ContentView: View {
@State private var time = Date()
@State private var fruit = "Apple"
let fruits = ["Apple", "Banana"]
var body: some View {
VStack {
DatePicker(selection: $time, displayedComponents: .hourAndMinute) { Text("Select Time") }
Picker(selection: $fruit) {
ForEach(fruits, id: \.self) { Text($0) }
} label: { Text("Select Fruit") }
}
}
}
B. struct ContentView: View {
@State private var time = Date()
@State private var fruit = "Apple"
let fruits = ["Apple", "Banana"]
var body: some View {
VStack {
DatePicker("Select Time", selection: time, displayedComponents: .hourAndMinute)
Picker("Select Fruit", selection: fruit) {
ForEach(fruits, id: \.self) { Text($0) }
}
}
}
}
C. struct ContentView: View {
@State private var time = Date()
@State private var fruit = "Apple"
let fruits = ["Apple", "Banana"]
var body: some View {
VStack {
DatePicker("Select Time", selection: time, displayedComponents: .hourAndMinute)
Picker("Select Fruit", selection: $fruit) {
ForEach(fruits, id: \.self) { Text($0) }
}
}
}
}
D. struct ContentView: View {
@State private var time = Date()
@State private var fruit = "Apple"
let fruits = ["Apple", "Banana"]
var body: some View {
VStack {
DatePicker("Select Time", selection: $time)
Picker("Select Fruit", selection: $fruit) {
ForEach(fruits, id: \.self) { Text($0) }
}
}
}
}
Solution
Step 1: Check DatePicker syntax with label closure
struct ContentView: View {
@State private var time = Date()
@State private var fruit = "Apple"
let fruits = ["Apple", "Banana"]
var body: some View {
VStack {
DatePicker(selection: $time, displayedComponents: .hourAndMinute) { Text("Select Time") }
Picker(selection: $fruit) {
ForEach(fruits, id: \.self) { Text($0) }
} label: { Text("Select Fruit") }
}
}
} uses DatePicker with selection binding and label closure, which is valid syntax.
Step 2: Check Picker syntax with label closure
Picker in struct ContentView: View {
@State private var time = Date()
@State private var fruit = "Apple"
let fruits = ["Apple", "Banana"]
var body: some View {
VStack {
DatePicker(selection: $time, displayedComponents: .hourAndMinute) { Text("Select Time") }
Picker(selection: $fruit) {
ForEach(fruits, id: \.self) { Text($0) }
} label: { Text("Select Fruit") }
}
}
} uses selection binding and label closure correctly, with ForEach and id: \.self.
Step 3: Verify other options
struct ContentView: View {
@State private var time = Date()
@State private var fruit = "Apple"
let fruits = ["Apple", "Banana"]
var body: some View {
VStack {
DatePicker("Select Time", selection: time, displayedComponents: .hourAndMinute)
Picker("Select Fruit", selection: $fruit) {
ForEach(fruits, id: \.self) { Text($0) }
}
}
}
} lacks $ for DatePicker selection binding, struct ContentView: View {
@State private var time = Date()
@State private var fruit = "Apple"
let fruits = ["Apple", "Banana"]
var body: some View {
VStack {
DatePicker("Select Time", selection: time, displayedComponents: .hourAndMinute)
Picker("Select Fruit", selection: fruit) {
ForEach(fruits, id: \.self) { Text($0) }
}
}
}
} lacks $ for bindings, struct ContentView: View {
@State private var time = Date()
@State private var fruit = "Apple"
let fruits = ["Apple", "Banana"]
var body: some View {
VStack {
DatePicker("Select Time", selection: $time)
Picker("Select Fruit", selection: $fruit) {
ForEach(fruits, id: \.self) { Text($0) }
}
}
}
} misses displayedComponents for time.
Final Answer:
The code using selection-first initializers with label closures -> Option A
Quick Check:
Use bindings with $ and label closures for both controls [OK]
Hint: Use $ for bindings and label closures for Picker and DatePicker [OK]
Common Mistakes:
Omitting $ in bindings
Missing label closures
Not specifying displayedComponents for time
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.