【发布时间】:2019-06-24 21:59:14
【问题描述】:
struct Objects {
var sectionName : String!
var sectionObjects : [CountryList]!
}
var objectArray = [Objects]()
这里objectArray 是我的tableView 数据源,其中sectionObjects 是CountryList struct 的数组。
struct CountryList: Codable {
let country_id: String?
let country_name: String?
let country_code: String?
let country_flag_url: String?
init(countryID: String, countryName: String, countryCode: String, countryFlagURL: String) {
self.country_id = countryID
self.country_name = countryName
self.country_code = countryCode
self.country_flag_url = countryFlagURL
}
}
我想根据country_name过滤我的objectArray。
这是我在UISearchResultsUpdating中所做的。
extension CountryListViewController: UISearchResultsUpdating {
public func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text else {return}
if searchText == "" {
objectArray += objectArray
} else {
objectArray += objectArray
objectArray = objectArray.filter {
let countryListArray = $0.sectionObjects!
for countryList in countryListArray {
print("cName \(String(describing: countryList.country_name))")
countryList.country_name!.contains(searchText)
}
}
}
self.countryListTableView.reloadData()
}
}
得到两个错误:
调用 'contains' 的结果未被使用
预期返回“Bool”的闭包中缺少返回
我在这里缺少什么?任何建议将不胜感激。
提前致谢。
【问题讨论】:
-
你的包含被隐藏在一个 for 循环中。
-
鉴于您的
init,如果不是,请不要将变量设为可选String?,因此请删除init方法和所有?尾随,然后您将得到结构的自动初始化
标签: arrays swift struct closures