NSPredicate没有处理某些对象

时间:2015-11-11 08:12:38

标签: ios swift swift2 nspredicate

我有数组包含模型,在该模型中我有3个对象,如字符串说,firstNamelastNameaction

我正在使用以下谓词

let resultPredicate = NSPredicate(format: "lastName CONTAINS [c] %@ OR firstName CONTAINS [c] %@ OR action CONTAINS [c] %@", searchText, searchText, searchText)            
tableDataArray = dataSourceArray.filter { resultPredicate.evaluateWithObject($0) }

如果我根据firstNamelastName搜索其工作正常的字符串。但是,如果我搜索action中存在的字符串,而不是过滤。 任何有关这方面的帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

真的很奇怪......我无法找到解决方案。但是我发现了在不使用谓词的情况下过滤数组的替代方法。

tableDataArray = dataSourceArray.filter {$0.firstName.containsString(searchText) || $0.lastName.containsString(searchText) || $0.action.containsString(searchText) }

答案 1 :(得分:0)

//Me to faced some issue, after many article i referred, found the solution

extension yourModelclassName {

    @objc override func value(forKey key: String) -> Any? {
        switch key {
        case "firstName":
            return firstName
        case "lastName":
            return lastName
        default:
            return nil
        }
    }
}

//Please put the given code in your model class like i gave above, it will work fine.