Challenge - 5 Problems
Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Searchable Modifier Filtering Behavior
Given this SwiftUI code snippet using the searchable modifier, what will be the displayed list items when the user types "ap" in the search bar?
iOS Swift
struct ContentView: View {
@State private var searchText = ""
let fruits = ["Apple", "Banana", "Grape", "Apricot", "Orange"]
var filteredFruits: [String] {
if searchText.isEmpty {
return fruits
} else {
return fruits.filter { $0.localizedCaseInsensitiveContains(searchText) }
}
}
var body: some View {
List(filteredFruits, id: \.self) { fruit in
Text(fruit)
}
.searchable(text: $searchText)
}
}Attempts:
2 left
💡 Hint
Think about how the filter uses localizedCaseInsensitiveContains with the search text.
✗ Incorrect
The filter returns fruits that contain the substring "ap" ignoring case. "Apple" and "Apricot" both contain "ap" ignoring case, so only these two appear.
❓ lifecycle
intermediate1:30remaining
Search Text State Update Timing
In SwiftUI, when using the searchable modifier with a @State searchText variable, when does the searchText update as the user types?
Attempts:
2 left
💡 Hint
Consider how SwiftUI binds the search bar text to the state variable.
✗ Incorrect
The searchable modifier binds the search bar text to the @State variable, updating it immediately as the user types each character.
advanced
2:00remaining
Searchable Modifier with NavigationView
What is the correct way to add a searchable modifier to a list inside a NavigationView so that the search bar appears in the navigation bar on iOS?
iOS Swift
struct ContentView: View {
@State private var searchText = ""
let items = ["One", "Two", "Three"]
var body: some View {
NavigationView {
List(items, id: \.self) { item in
Text(item)
}
// Where to place searchable modifier?
}
}
}Attempts:
2 left
💡 Hint
The searchable modifier must be attached to the view that produces the list content inside the navigation.
✗ Incorrect
The searchable modifier should be applied to the List inside the NavigationView to show the search bar in the navigation bar area.
📝 Syntax
advanced1:30remaining
Correct Syntax for searchable Modifier with Prompt
Which option shows the correct syntax to add a searchable modifier with a prompt text "Search fruits" in SwiftUI?
Attempts:
2 left
💡 Hint
Check the parameter names and use of $ for binding.
✗ Incorrect
The searchable modifier requires a binding to a String with $ and the prompt parameter is named prompt, not placeholder.
🔧 Debug
expert2:30remaining
Why Does Searchable Not Show Search Bar?
You added .searchable(text: $searchText) to your List inside a NavigationView, but the search bar does not appear on iOS 14. What is the reason?
Attempts:
2 left
💡 Hint
Check the iOS version compatibility of the searchable modifier.
✗ Incorrect
The searchable modifier was introduced in iOS 15, so it does not work on iOS 14 or earlier versions.