如何在 NSPredicate 的帮助下使用自定义对象数组实现搜索?
How to implement search using array of custom objects with help of NSPredicate?
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"
那么结果应该 return alex 和 alena 的对象
你可以只使用平面地图然后过滤
FlatMap 将加入所有部分过滤器将使用您的密钥对其进行过滤
let results = contactsWithSections.flatMap {[=10=]}.filter { (obj) -> Bool in
guard let keyValue = obj.fName else {return false}
return keyValue == searchStr
}
找到了与我的问题完美匹配的更相关的解决方案
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
}
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"
那么结果应该 return alex 和 alena 的对象
你可以只使用平面地图然后过滤
FlatMap 将加入所有部分过滤器将使用您的密钥对其进行过滤
let results = contactsWithSections.flatMap {[=10=]}.filter { (obj) -> Bool in
guard let keyValue = obj.fName else {return false}
return keyValue == searchStr
}
找到了与我的问题完美匹配的更相关的解决方案
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
}