Bird
Raised Fist0
iOS Swiftmobile~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the searchable modifier do in a SwiftUI view?
easy
A. Removes all items from the list when typing.
B. Automatically sorts the list items alphabetically.
C. Changes the background color of the list.
D. Adds a search bar to filter content in lists or scroll views.

Solution

  1. Step 1: Understand the purpose of searchable

    The searchable modifier adds a search bar UI element to a list or scroll view.
  2. Step 2: Identify its effect on content

    It allows users to type text to filter or search through the displayed content.
  3. Final Answer:

    Adds a search bar to filter content in lists or scroll views. -> Option D
  4. Quick Check:

    searchable = search bar added [OK]
Hint: searchable always adds a search bar UI [OK]
Common Mistakes:
  • Thinking searchable sorts items automatically
  • Confusing searchable with styling modifiers
  • Assuming searchable removes items without filtering
2. Which of the following is the correct syntax to add a searchable modifier with a binding to a @State variable named searchText?
easy
A. .searchable(searchText)
B. .searchable($searchText)
C. .searchable(text: $searchText)
D. .searchable(text: searchText)

Solution

  1. Step 1: Recall the searchable syntax

    The searchable modifier requires a binding to a string, written as text: $variable.
  2. Step 2: Match the correct binding syntax

    Using $searchText passes the binding, so the correct syntax is .searchable(text: $searchText).
  3. Final Answer:

    .searchable(text: $searchText) -> Option C
  4. Quick Check:

    Binding uses $ with variable name [OK]
Hint: Use $ to bind @State variable in searchable [OK]
Common Mistakes:
  • Omitting the text: label
  • Not using $ for binding
  • Passing variable without binding
3. Given this SwiftUI code snippet, what will be displayed when the user types "ap" in the search bar?
struct ContentView: View {
  @State private var searchText = ""
  let fruits = ["Apple", "Banana", "Grape", "Apricot"]

  var filteredFruits: [String] {
    if searchText.isEmpty { return fruits }
    return fruits.filter { $0.lowercased().contains(searchText.lowercased()) }
  }

  var body: some View {
    List(filteredFruits, id: \.self) { fruit in
      Text(fruit)
    }
    .searchable(text: $searchText)
  }
}
medium
A. ["Apple", "Apricot"]
B. ["Banana", "Grape"]
C. ["Apple", "Banana", "Grape", "Apricot"]
D. Empty list

Solution

  1. Step 1: Understand filtering logic

    The filter checks if each fruit contains the search text "ap" ignoring case.
  2. Step 2: Apply filter to fruits list

    "Apple" and "Apricot" contain "ap" (case-insensitive), "Banana" and "Grape" do not match exactly.
  3. Final Answer:

    ["Apple", "Apricot"] -> Option A
  4. Quick Check:

    Filter by contains "ap" = Apple, Apricot [OK]
Hint: Filter checks contains ignoring case [OK]
Common Mistakes:
  • Including fruits that don't contain "ap"
  • Ignoring case sensitivity
  • Returning full list regardless of search
4. Identify the error in this SwiftUI code using searchable:
struct ContentView: View {
  @State var searchText = ""
  let items = ["One", "Two", "Three"]

  var filteredItems: [String] {
    items.filter { $0.contains(searchText) }
  }

  var body: some View {
    List(filteredItems, id: \.self) { item in
      Text(item)
    }
    .searchable(text: searchText)
  }
}
medium
A. Missing $ to bind searchText in searchable modifier.
B. filteredItems should be a @State variable.
C. List requires a ForEach instead of direct array.
D. searchText should be a constant, not @State.

Solution

  1. Step 1: Check searchable binding syntax

    The searchable modifier requires a binding, so it must be text: $searchText, not text: searchText.
  2. Step 2: Verify other code parts

    filteredItems is computed correctly, List accepts array with id, and searchText should be @State.
  3. Final Answer:

    Missing $ to bind searchText in searchable modifier. -> Option A
  4. Quick Check:

    Binding needs $ prefix [OK]
Hint: Always use $ for @State binding in searchable [OK]
Common Mistakes:
  • Forgetting $ in searchable binding
  • Making filteredItems @State unnecessarily
  • Confusing List requirements
5. You want to create a searchable list of contacts where typing filters by first or last name. Which code snippet correctly implements this behavior?
struct Contact: Identifiable {
  let id = UUID()
  let firstName: String
  let lastName: String
}

struct ContentView: View {
  @State private var searchText = ""
  let contacts = [Contact(firstName: "John", lastName: "Doe"), Contact(firstName: "Jane", lastName: "Smith"), Contact(firstName: "Alice", lastName: "Johnson")]

  var filteredContacts: [Contact] {
    if searchText.isEmpty { return contacts }
    return contacts.filter { contact in
      contact.firstName.lowercased().contains(searchText.lowercased()) || contact.lastName.lowercased().contains(searchText.lowercased())
    }
  }

  var body: some View {
    List(filteredContacts) { contact in
      Text("\(contact.firstName) \(contact.lastName)")
    }
    .searchable(text: $searchText)
  }
}
hard
A. Filters contacts only by first name and ignores last name.
B. Filters contacts by first or last name ignoring case and updates list dynamically.
C. Filters contacts but does not ignore case, so search is case-sensitive.
D. Does not filter contacts and always shows full list.

Solution

  1. Step 1: Analyze filtering logic

    The filter checks if either firstName or lastName contains the search text, ignoring case.
  2. Step 2: Confirm UI updates with searchable

    The searchable modifier binds to $searchText, so typing updates the filteredContacts list dynamically.
  3. Final Answer:

    Filters contacts by first or last name ignoring case and updates list dynamically. -> Option B
  4. Quick Check:

    Filter by first or last name, case-insensitive [OK]
Hint: Filter with OR condition and ignore case for full search [OK]
Common Mistakes:
  • Filtering only by first name
  • Not using lowercased() causing case sensitivity
  • Forgetting to bind searchText with $
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard