3
私のアプリケーションで検索機能を実装したいが、サービスからデータを取得する。私はオブジェクトマッパーでこのような配列を持っていますオブジェクトマッパークラスからデータをフィルタリングする方法
class Country : Mappable {
var countryName:String = ""
var countryID:Int = 0
var countryImage:String = ""
var countryColor:String = ""
required init?(_ map: Map) {
}
func mapping(map: Map) {
countryID <- map["id"]
countryName <- map["name"]
countryColor <- map["color"]
countryImage <- map["image"]
}
}
ここから私は検索機能のためにデータをフィルタリングしたいと思います。
ここで私は唯一の国の名前をフィルタリングしていますが、私は
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filteredData = self.countryNames.filter { (country:String) -> Bool in
if country.lowercaseString.containsString(self.searchController.searchBar.text!.lowercaseString) {
return true
} else {
return false
}
}
print(filteredData)
// update results table view
self.resultController.tableView.reloadData()
}
あなたは国の配列['[Country]'をフィルタリングしたいのですか? –
ええ、私は全体の配列をフィルタリングしたい@NiravD –
それから、単純に 'let filter = countries.filter {$ 0.countryName.lowercaseString.containsString(self.searchController.searchBar.text!.lowercaseString)}' –