所以我不知道为什么,但我有一个自定义对象筛选通过自定义阵列
struct Country {
id: Int,
name: String
}
//List of Countries
dataArray = [Country]()
//Error: "Cannot invoke filter with an arg list of type ((Country)) throws -> Bool
filteredArray = dataArray.filter({ (country) -> Bool in
let countryText:NSString = country.name as NSString
return (countryText.range(of: searchString, options: NSString.CompareOptions.caseInsensitive).location) != NSNotFound
})
如果dataArray中是一个字符串列表,而不是那么它会工作,我只是不明白为什么,看着其他SO问题,我返回一个布尔
Filter array of custom objects in Swift
Swift 2.0 filtering array of custom objects - Cannot invoke 'filter' with an argument of list type
你误解了'filter'方法。因为封闭的返回意味着:“我必须将该项目放入返回的数组中吗?”?是的,它被添加,否则,它不会被添加。根据该项目,然后由您来做适当的测试:就像在您的案例中对其名称属性进行测试一样。 – Larme