swift 2.0 中过滤方法的第二个参数
second parameter for filter method in swift 2.0
我使用以下代码在我的应用程序中搜索 tableviewcontroller。
当前搜索字符串与我的 table 列表中的餐馆名称匹配。我希望 match/search 附加字符串,位置与我的代码。如何向此功能添加位置
餐厅名称变量 - restaurant.name ,
餐厅位置变量 - restaurant.location
func filterContentForSearchText(searchText: String) {
searchResults = restaurants.filter({ ( restaurant: Restaurant) -> Bool in let nameMatch = restaurant.name.rangeOfString(searchText, options:
NSStringCompareOptions.CaseInsensitiveSearch)
return nameMatch != nil
})
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchText = searchController.searchBar.text
filterContentForSearchText(searchText!)
tableView.reloadData()
}
如何在我的过滤方法中添加这个附加参数?
试试这个。
func filterContentForSearchText(searchText: String) {
searchResults = restaurants.filter({ ( restaurant: Restaurant) -> Bool in
let nameMatch = restaurant.name.rangeOfString(searchText, options:
NSStringCompareOptions.CaseInsensitiveSearch)
let locationMatch = restaurant.location.rangeOfString(searchText, options:
NSStringCompareOptions.CaseInsensitiveSearch)
return (nameMatch != nil || locationMatch != nil)
})
}
You can do like this
func filterContentForSearchText(searchText: String) {
searchResults = restaurants.filter({( restaurant: Restaurant) -> Bool in
return restaurant.location.lowercaseString.containsString(searchText.lowercaseString) || restaurant.name.lowercaseString.containsString(searchText.lowercaseString)
})
tableView.reloadData()
}
Delegate Method
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
此方法将帮助您更好地搜索
我使用以下代码在我的应用程序中搜索 tableviewcontroller。 当前搜索字符串与我的 table 列表中的餐馆名称匹配。我希望 match/search 附加字符串,位置与我的代码。如何向此功能添加位置
餐厅名称变量 - restaurant.name ,
餐厅位置变量 - restaurant.location
func filterContentForSearchText(searchText: String) {
searchResults = restaurants.filter({ ( restaurant: Restaurant) -> Bool in let nameMatch = restaurant.name.rangeOfString(searchText, options:
NSStringCompareOptions.CaseInsensitiveSearch)
return nameMatch != nil
})
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchText = searchController.searchBar.text
filterContentForSearchText(searchText!)
tableView.reloadData()
}
如何在我的过滤方法中添加这个附加参数?
试试这个。
func filterContentForSearchText(searchText: String) {
searchResults = restaurants.filter({ ( restaurant: Restaurant) -> Bool in
let nameMatch = restaurant.name.rangeOfString(searchText, options:
NSStringCompareOptions.CaseInsensitiveSearch)
let locationMatch = restaurant.location.rangeOfString(searchText, options:
NSStringCompareOptions.CaseInsensitiveSearch)
return (nameMatch != nil || locationMatch != nil)
})
}
You can do like this
func filterContentForSearchText(searchText: String) {
searchResults = restaurants.filter({( restaurant: Restaurant) -> Bool in
return restaurant.location.lowercaseString.containsString(searchText.lowercaseString) || restaurant.name.lowercaseString.containsString(searchText.lowercaseString)
})
tableView.reloadData()
}
Delegate Method
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
此方法将帮助您更好地搜索