0
0
iOS Swiftmobile~20 mins

Search with searchable modifier in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)
  }
}
A["Grape", "Apricot"]
B["Apple", "Banana", "Grape", "Apricot", "Orange"]
C["Apple", "Apricot"]
D["Banana", "Orange"]
Attempts:
2 left
💡 Hint
Think about how the filter uses localizedCaseInsensitiveContains with the search text.
lifecycle
intermediate
1: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?
AsearchText updates only when the view reloads
BsearchText updates only after the user presses Return
CsearchText updates only when the search bar loses focus
DsearchText updates immediately on every character typed
Attempts:
2 left
💡 Hint
Consider how SwiftUI binds the search bar text to the state variable.
navigation
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?
    }
  }
}
APlace .searchable(text: $searchText) directly on the List inside NavigationView
BPlace .searchable(text: $searchText) on the NavigationView itself
CPlace .searchable(text: $searchText) outside NavigationView wrapping the whole view
DPlace .searchable(text: $searchText) on the Text inside the List
Attempts:
2 left
💡 Hint
The searchable modifier must be attached to the view that produces the list content inside the navigation.
📝 Syntax
advanced
1: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?
A.searchable($searchText, placeholder: "Search fruits")
B.searchable(text: $searchText, prompt: "Search fruits")
C.searchable(text: searchText, prompt: "Search fruits")
D.searchable(text: $searchText, placeholder: "Search fruits")
Attempts:
2 left
💡 Hint
Check the parameter names and use of $ for binding.
🔧 Debug
expert
2: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?
AThe searchable modifier is only available on iOS 15 and later
BYou forgot to add a NavigationLink inside the List
CThe searchText variable is not marked with @State
DThe List must be wrapped in a VStack for searchable to work
Attempts:
2 left
💡 Hint
Check the iOS version compatibility of the searchable modifier.