如何通过UISearchBar文本过滤内容?

时间:2018-10-24 17:22:23

标签: swift uisearchbar uisearchbardelegate uisearchbardisplaycontrol

我使用此代码进行搜索

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()
            }

        }
    }
}

我不断得到

  

通话中的其他参数

有人知道我错过了什么以及如何阻止它吗?


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"]]

1 个答案:

答案 0 :(得分:1)

您有一个Array。您正在尝试使用filter,但您的参数或更适合NSPredicate。正确使用filter

places = places.filter { ($0["name"] ?? "").range(of: searchBar.text, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil }