Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a search bar to the list.
iOS Swift
List(items, id: \.self) { item in
Text(item)
}
.[1](text: $searchText) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'search' instead of 'searchable' causes a compiler error.
Trying to use 'searchBar' which is not a SwiftUI modifier.
✗ Incorrect
The searchable modifier adds a search bar to the list.
2fill in blank
mediumComplete the code to declare the search text state variable.
iOS Swift
@State private var [1] = ""
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name without updating the binding causes the search bar not to work.
✗ Incorrect
The variable searchText is commonly used to hold the search input.
3fill in blank
hardFix the error in filtering the list based on search text.
iOS Swift
let filteredItems = items.filter { item in
item.lowercased().contains([1].lowercased())
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not declared causes a compile error.
Using the wrong variable name breaks the filtering logic.
✗ Incorrect
The filter uses searchText to check if the item contains the search input.
4fill in blank
hardFill both blanks to create a filtered list that updates with search text.
iOS Swift
List([1]) { item in Text(item) } .searchable(text: $[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Showing all items instead of filtered items.
Binding searchable to a wrong variable.
✗ Incorrect
The list shows filteredItems and binds the search bar to searchText.
5fill in blank
hardFill all three blanks to complete the search filtering logic.
iOS Swift
let filteredItems = [1].filter { item in item.[2]().contains(searchText.[3]()) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong array name causes errors.
Not using consistent casing causes search mismatches.
✗ Incorrect
The filtering uses items and compares lowercase strings for case-insensitive search.