问题
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
回答1:
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()
}
来源:https://stackoverflow.com/questions/57800914/how-do-i-filter-data-from-database-arrays-using-search-bar