Bird
0
0

Identify the error in this SwiftUI code using searchable:

medium📝 Debug Q14 of 15
iOS Swift - Lists and Data Display
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)
  }
}
AMissing $ to bind searchText in searchable modifier.
BfilteredItems should be a @State variable.
CList requires a ForEach instead of direct array.
DsearchText should be a constant, not @State.
Step-by-Step Solution
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]
Quick Trick: Always use $ for @State binding in searchable [OK]
Common Mistakes:
  • Forgetting $ in searchable binding
  • Making filteredItems @State unnecessarily
  • Confusing List requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes