NSPredicate - 针对多种情况的不区分大小写的过滤

时间:2014-11-10 06:29:04

标签: ios objective-c cocoa-touch nsmutablearray nspredicate

我正在使用NSPredicate进行过滤,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(firstName CONTAINS %@ ) OR (lastName CONTAINS %@  )OR (UserName CONTAINS %@ )  ", myText,myText,myText];
NSArray *filtered = [responseArray filteredArrayUsingPredicate:predicate];

它工作正常,但它区分大小写。我需要过滤应该不区分大小写。

我试过了,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY firstName CONTAINS %@ ) OR (ANY lastName CONTAINS %@  )OR (ANY UserName CONTAINS %@ )  ", myText,myText,myText];

但它会抛出错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet.'

我无法理解this answer中提到的内容。任何人都可以建议我在上面的过滤中应该改变什么?

1 个答案:

答案 0 :(得分:16)

将所有CONTAINS替换为CONTAINS[c]。方括号中的“c”表示不区分大小写。您还可以使用“d”使其变音符号不敏感。这在“谓词编程指南”中有解释。