Challenge - 5 Problems
Picker and DatePicker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this SwiftUI Picker code?
Consider this SwiftUI code snippet for a Picker. What will the user see and how will the Picker behave?
iOS Swift
struct ContentView: View {
@State private var selectedColor = "Red"
let colors = ["Red", "Green", "Blue"]
var body: some View {
Picker("Select a color", selection: $selectedColor) {
ForEach(colors, id: \.self) { color in
Text(color).tag(color)
}
}
.pickerStyle(.wheel)
}
}Attempts:
2 left
💡 Hint
Look at the .pickerStyle modifier to understand the Picker's appearance.
✗ Incorrect
The .wheel picker style creates a spinning wheel UI. The ForEach creates options from the colors array. The selection binding updates the selectedColor state.
❓ ui_behavior
intermediate2:00remaining
What happens when you change the DatePicker's displayedComponents?
Given this SwiftUI DatePicker code, what will the user see if displayedComponents is set to [.date] only?
iOS Swift
struct ContentView: View {
@State private var selectedDate = Date()
var body: some View {
DatePicker("Select date and time", selection: $selectedDate, displayedComponents: [.date])
.datePickerStyle(.compact)
}
}Attempts:
2 left
💡 Hint
Check the displayedComponents parameter to control what parts of date/time are shown.
✗ Incorrect
Setting displayedComponents to [.date] restricts the picker to show only the date selection UI. Time selection is hidden.
❓ lifecycle
advanced2:00remaining
What is the effect of changing the @State variable bound to a Picker?
In SwiftUI, if you programmatically change the @State variable bound to a Picker's selection, what happens to the Picker's UI?
iOS Swift
struct ContentView: View {
@State private var selectedFruit = "Apple"
let fruits = ["Apple", "Banana", "Cherry"]
var body: some View {
VStack {
Picker("Select fruit", selection: $selectedFruit) {
ForEach(fruits, id: \.self) { fruit in
Text(fruit).tag(fruit)
}
}
Text("You selected: \(selectedFruit)")
Button("Select Banana") {
selectedFruit = "Banana"
}
}
}
}Attempts:
2 left
💡 Hint
Think about how SwiftUI updates views when state changes.
✗ Incorrect
Changing the @State variable triggers SwiftUI to re-render the Picker with the new selection reflected immediately.
📝 Syntax
advanced2:00remaining
What error does this DatePicker code produce?
Examine this SwiftUI DatePicker code. What error will it cause?
iOS Swift
struct ContentView: View {
@State private var date = Date()
var body: some View {
DatePicker("Pick a date", selection: $date, displayedComponents: .date)
}
}Attempts:
2 left
💡 Hint
Look carefully at the type expected for displayedComponents parameter.
✗ Incorrect
The displayedComponents parameter expects a Set of DatePickerComponents, but .date alone is a single value, not a Set.
🧠 Conceptual
expert3:00remaining
Why use a Picker over a DatePicker for selecting options?
In iOS SwiftUI, when should you prefer using a Picker instead of a DatePicker?
Attempts:
2 left
💡 Hint
Think about the purpose of each control and the type of data they handle.
✗ Incorrect
Picker is designed for choosing from a list of discrete options. DatePicker is specialized for selecting dates and times.