如何通过 UISearchBar 文本过滤掉内容?
How can I filter out contents via UISearchBar text?
我用这个代码搜索
extension PlacesVC : UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
places = places.filter("name CONTAINS[cd] %@", searchBar.text)
table.reloadData()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text?.count == 0 {
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
}
}
我不断收到
Extra argument in call
有谁知道我错过了什么以及如何阻止它?
var places = [["lat": "40.718081110553925", "name": "59 Leonard St", "lon": "-74.00565161821724"], ["lat": "40.87124295389947", "name": "66 Lawrence Ave", "lon": "-74.0764477654279"], ["lat": "40.82326949018643", "name": "685 Fairview Ave", "lon": "-74.00056756991113"]]
你有一个Array
。您正在尝试使用 filter
,但您的参数或更适合 NSPredicate
。正确使用filter
。
places = places.filter { ([=10=]["name"] ?? "").range(of: searchBar.text, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil }
我用这个代码搜索
extension PlacesVC : UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
places = places.filter("name CONTAINS[cd] %@", searchBar.text)
table.reloadData()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text?.count == 0 {
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
}
}
我不断收到
Extra argument in call
有谁知道我错过了什么以及如何阻止它?
var places = [["lat": "40.718081110553925", "name": "59 Leonard St", "lon": "-74.00565161821724"], ["lat": "40.87124295389947", "name": "66 Lawrence Ave", "lon": "-74.0764477654279"], ["lat": "40.82326949018643", "name": "685 Fairview Ave", "lon": "-74.00056756991113"]]
你有一个Array
。您正在尝试使用 filter
,但您的参数或更适合 NSPredicate
。正确使用filter
。
places = places.filter { ([=10=]["name"] ?? "").range(of: searchBar.text, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil }