使用NSPredicate搜索一系列NSStrings

时间:2014-11-01 11:10:36

标签: ios objective-c nspredicate

我正在尝试使用NSPredicate搜索Core Data以查找与NSStrings个匹配的所有结果。以下工作用于使用'a'查找结果,但如何设置NSPredicate以找到'a','b'和'c'?

NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"Result ==  'a'"]];

2 个答案:

答案 0 :(得分:3)

NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"Result ==  'a' OR Result ==  'b' OR Result ==  'c'"]];

OR

  

编辑:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.Result IN %@",[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];

答案 1 :(得分:0)

搜索字符串的另一种方法是使用Regular Expression。试试这个: -

NSString *yourString = @"Insert your String here";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression         
    regularExpressionWithPattern:@"[a|b|c]"
    options:NSRegularExpressionCaseInasensitive
    error:&error];
[regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
    // your code here
}];