How do I filter a list using the search bar in swiftui

271 Views Asked by At

How do I filter a list using a search bar in SwiftUI.

This is what I have done so far, it shows all the information in the list but the list does not filter the information.

ForEach(
  (0..<self.appData.myJobs.count).filter(
    { "\($0)".contains(searchBar.text) || searchBar.text.isEmpty  }), 
  id: \.self) { index in

Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

Now you try to filter indices (like 1, 2, 3...), but I assume you try to filter by some texts, so it should be something like (I don't now your Job properties, so it is just assumption, but idea should be clear)

ForEach(
  (0..<self.appData.myJobs.count).filter({ i in // << here you get index 
      // assuming you have .jobTitle property in Job and want to filter by it
      // so just substitute your property
      self.appData.myJobs[i].jobTitle.contains(searchBar.text) || 
      searchBar.text.isEmpty }), 
  id: \.self) { index in