private var contactsWithSections = [[WPH]]()
WPH具有以下属性:
class WPH: NSObject {
var fName: String? = nil
var lName: String? = nil
var number: String? = nil
var email: String? = nil
}
我想基于fName进行搜索,即在数组中有fname = alex,fname = alena,fname = flora
let searchStr = "al"
然后结果应返回alex和alena的对象
答案 0 :(得分:1)
找到与我的问题完美匹配的更相关的解决方案
let filtered = MasterList!.filter { (data) -> Bool in
return data.fName?.range(of: Key, options: String.CompareOptions.caseInsensitive) != nil || data.lName?.range(of: Key, options: String.CompareOptions.caseInsensitive) != nil
}
答案 1 :(得分:0)
您可以使用平面地图,然后使用过滤器
FlatMap将加入所有部分过滤器,将使用您的密钥
过滤它let results = contactsWithSections.flatMap {$0}.filter { (obj) -> Bool in
guard let keyValue = obj.fName else {return false}
return keyValue == searchStr
}