Using searchable lists
List views can hold from one to an uncountable number of items. As the number of items in a list increases, it is usually helpful to provide users with the ability to search through the list for a specific item without having to scroll through the whole list. In this recipe, we'll introduce the searchable(text:placement:prompt:)
modifier, which marks the view as searchable and configures the display of a search field, and discuss how it can be used to search through items in a list.
Getting ready
Create a new SwiftUI project and name it SearchableLists
.
How to do it…
Let's create an app to search through different types of food. The steps are as follows:
- Add a new Swift file to the project called
Food
, which will contain the data used for the project. We use a struct to model different types of food. The struct has two properties, a String with the name of the food, and a category, which is anenum
representing the type of food:
struct Food...