Xcode 如何使用搜索栏从数据库数组中筛选数据
Im使用搜索栏过滤数据库中的数据,并填充tableView。我在搜索部分出错了Xcode 如何使用搜索栏从数据库数组中筛选数据,xcode,search,filter,Xcode,Search,Filter,Im使用搜索栏过滤数据库中的数据,并填充tableView。我在搜索部分出错了 Value of type 'DataSnapshot' has no member 'contains' 这是密码 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { if searchBar.text == nil || searchBar.text == "" { isSearchin
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()
}
}
如何您当然希望搜索特定的
字符串
属性,例如名称
使用searchText
参数,而不是从搜索栏获取搜索字符串,该参数已经是非可选的
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()
}
你好,瓦迪亚人。当我尝试键入某个内容时,它不会显示任何内容。我已经更新了cellForRowAt
名称
只是一个占位符。我不知道快照包含什么。它在@vadian工作。谢谢我还有一个问题。您使用什么代码过滤与当前用户的距离。就像我想在名单上显示最接近最远的人一样。我有一个经度和纬度的即时快照。使用CoreLocation
的API。从lat/long创建CLLocation
对象并计算两个位置之间的距离很容易。将.contains(searchText)
替换为。范围(of:searchText,选项:。不区分大小写)!=无