如何使用搜索栏从数据库数组中过滤数据 [英] How do I filter data from database arrays using Search bar

查看:76
本文介绍了如何使用搜索栏从数据库数组中过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用搜索栏从数据库中筛选数据,并将填充tableView.我在isSearching部分遇到错误.

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'

这里有代码.

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

如何

推荐答案

您当然想搜索特定的String属性,例如name.

You certainly want to search for a specific String property for example a name.

使用searchText参数而不是从条形中获取搜索字符串,该参数已经是非可选的.

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

这篇关于如何使用搜索栏从数据库数组中过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆