我想知道如何使用filter
NSDictionary
searchBar
?
我声明了2个变量var usersArray = [NSDictionary?]()
和var filteredUsers = [NSDictionary?]()
现在我试图像这样过滤它们:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.filteredUsers = self.usersArray.filter{ user in
let username = user!["username"] as? String
return(username?.lowercased().contains(searchText.lowercased()))!
}
if searchText != ""{
shouldShowSearchResults = true
followUsersTableView.reloadData()
}else{
shouldShowSearchResults = false
followUsersTableView.reloadData()
}
}
我做了updateSearchResults
:
filteredUsers.removeAll(keepingCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (usersArray as NSDictionary).filtered(using: searchPredicate)
filteredUsers = array as! [String]
self.followUsersTableView.reloadData()
但由于某种原因,它没有过滤它。
我该怎么做?