0

Im using the search bar to filter data from the database and will populate the tableView. Im getting an error on the isSearching part.

Value of type 'DataSnapshot' has no member 'contains'

Heres the code.

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchBar.text == nil || searchBar.text == "" {
        isSearching = false
        view.endEditing(true)
        tableView.reloadData()
    } else {
        isSearching = true
        filteredColorRequests = colors.filter{$0.contains(searchBar.text!)}
        tableView.reloadData()
    }
}

How

Nick
  • 104
  • 11

1 Answers1

0

You certainly want to search for a specific String property for example a name.

And rather than getting the search string form the bar use the searchText parameter which is already non-optional.

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText.isEmpty {
        isSearching = false
        view.endEditing(true)
    } else {
        isSearching = true
        filteredColorRequests = colors.filter{(($0.value as! [String:Any])["name"] as! String).contains(searchText)}
    }
    tableView.reloadData()
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Hi @vadian. when i try to type something it doesnt show anything. I've already updated the cellForRowAt – Nick Sep 05 '19 at 08:46
  • `name` is just a placeholder. I have no idea what the snapshot contains. – vadian Sep 05 '19 at 08:48
  • its working @vadian. thanks! i have one more question. what code do you use to filter the distance from the current user. Like i want to show on the list the nearest person to the furthest. i have im my snapshot the userA Longitude and latitude. – Nick Sep 05 '19 at 08:53
  • Use the API of `CoreLocation`. It’s easy to create `CLLocation` objects from lat/long and calculate the distance between two locations. – vadian Sep 05 '19 at 08:56
  • im so amazed! the searchBar is now working @vadian do you mind helping me with this one too? https://stackoverflow.com/questions/57799390/how-to-make-a-expandable-side-menu-tableview-list – Nick Sep 05 '19 at 09:13
  • how do i make this case insensitive? – Nick Sep 07 '19 at 04:46
  • 1
    Replace `.contains(searchText)` with `.range(of: searchText, options: .caseInsensitive) != nil` – vadian Sep 07 '19 at 04:48