How do I filter data from database arrays using Search bar

六月ゝ 毕业季﹏ 提交于 2020-03-04 21:24:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!