【问题标题】:NSPredicate: Combine CONTAINS with INNSPredicate:将 CONTAINS 与 IN 结合
【发布时间】:2012-11-20 18:16:46
【问题描述】:

我在 CoreData 中有一组用户,在我的应用程序中有一个搜索字段。用户具有属性 firstname 和 name。

目前我有一个谓词,例如“user.name CONTAINS[c] %@ OR user.firstname CONTAINS[c] %@”

在用户输入全名(如“john smith”)之前,此方法有效。即使他输入“john sm”,也应该找到 John Smith-Object。

将搜索词数组 (IN) 与 CONTAINS 组合的谓词是什么?

【问题讨论】:

标签: core-data nspredicate


【解决方案1】:

我认为您不能在谓词中将“IN”与“CONTAINS”结合起来。但是您可以将搜索字符串拆分为单词,并创建一个“复合谓词”:

NSString *searchString = @"John  Sm ";
NSArray *words = [searchString componentsSeparatedByString:@" "];
NSMutableArray *predicateList = [NSMutableArray array];
for (NSString *word in words) {
    if ([word length] > 0) {
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"user.name CONTAINS[c] %@ OR user.firstname CONTAINS[c] %@", word, word];
        [predicateList addObject:pred];
    }
}
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicateList];
NSLog(@"%@", predicate);

这个例子产生了谓词

(user.name CONTAINS[c] "John" OR user.firstname CONTAINS[c] "John") AND
(user.name CONTAINS[c] "Sm" OR user.firstname CONTAINS[c] "Sm")

将匹配“John Smith”,但不匹配“John Miller”。

【讨论】:

【解决方案2】:

2.5 年后,我可以在 swift 中用一个更复杂的例子来回答我的问题:

var predicateList = [NSPredicate]()

let words = filterText.componentsSeparatedByString(" ")

for word in words{

     if count(word)==0{
           continue
     }

     let firstNamePredicate = NSPredicate(format: "firstName contains[c] %@", word)
     let lastNamePredicate = NSPredicate(format: "lastName contains[c] %@", word)
     let departmentPredicate = NSPredicate(format: "department contains[c] %@", word)
     let jobTitlePredicate = NSPredicate(format: "jobTitle contains[c] %@", word)

     let orCompoundPredicate = NSCompoundPredicate(type: NSCompoundPredicateType.OrPredicateType, subpredicates: [firstNamePredicate, lastNamePredicate,departmentPredicate,jobTitlePredicate])

     predicateList.append(orCompoundPredicate)
}

request.predicate = NSCompoundPredicate(type: NSCompoundPredicateType.AndPredicateType, subpredicates: predicateList)

【讨论】:

【解决方案3】:

Swift 4 更新:

let firstName = NSPredicate(format: "firstName CONTAINS[c] %@", searchText)
let lastName = NSPredicate(format: "lastName CONTAINS[c] %@", searchText)

let orCompoundPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: 
[firstNamePredicate,lastNamePredicate]

在获取数据时使用 orCompoundPredicate

【讨论】:

【解决方案4】:

我遇到了同样的问题并这样解决了:

在我的 NSManagedObject 类(用户)中,我添加了以下方法,将两个值合并为一个字符串:

- (NSString *)name
{
    return [NSString stringWithFormat:@"%@ %@", self.firstname, self.lastname];
}

那么你只需要以下几行来匹配你的列表,在我的例子中是一个带有用户对象的 NSArray:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchString];
NSArray *filteredUsers = [users filteredArrayUsingPredicate:predicate];

【讨论】:

  • 这并不能解决用户搜索“lastname firstname”时的问题
猜你喜欢
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 2021-05-26
  • 2010-12-21
  • 1970-01-01
  • 2012-07-13
  • 2010-11-21
  • 1970-01-01
相关资源
最近更新 更多