0
0
iOS Swiftmobile~20 mins

Picker and DatePicker in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Picker and DatePicker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)
  }
}
AA segmented control with three buttons labeled Red, Green, and Blue. The user taps to select a color.
BA list of colors displayed vertically. The user taps a color to select it.
CA text field where the user types the color name manually.
DA wheel-style picker showing Red, Green, and Blue. The user can spin the wheel to select a color. The selected color updates the state variable.
Attempts:
2 left
💡 Hint
Look at the .pickerStyle modifier to understand the Picker's appearance.
ui_behavior
intermediate
2: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)
  }
}
AThe user sees a compact date picker that lets them pick only the date, not the time.
BThe user sees a time-only picker without date selection.
CThe user sees a picker that lets them select both date and time.
DThe picker shows no UI because displayedComponents is invalid.
Attempts:
2 left
💡 Hint
Check the displayedComponents parameter to control what parts of date/time are shown.
lifecycle
advanced
2: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"
      }
    }
  }
}
AThe app crashes because the state variable is changed programmatically.
BThe Picker UI does not change until the user interacts with it.
CThe Picker UI updates immediately to show Banana as selected.
DThe Picker resets to the first item in the list.
Attempts:
2 left
💡 Hint
Think about how SwiftUI updates views when state changes.
📝 Syntax
advanced
2: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)
  }
}
ANo error, code compiles and runs correctly.
BType error: Cannot convert value of type 'DatePickerComponents' to expected argument type 'Set<DatePickerComponents>'
CSyntax error: Missing parentheses around .date
DRuntime error: DatePicker crashes when displayedComponents is not a set.
Attempts:
2 left
💡 Hint
Look carefully at the type expected for displayedComponents parameter.
🧠 Conceptual
expert
3: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?
AUse Picker when selecting from a fixed list of options like colors or fruits; use DatePicker only for dates and times.
BUse Picker only for dates; DatePicker is for general lists.
CUse DatePicker when you want a wheel UI for any selection, Picker for buttons only.
DPicker and DatePicker are interchangeable and can be used for any selection.
Attempts:
2 left
💡 Hint
Think about the purpose of each control and the type of data they handle.