我正在尝试使用NSPredicate
搜索Core Data
以查找与NSStrings
个匹配的所有结果。以下工作用于使用'a'查找结果,但如何设置NSPredicate以找到'a','b'和'c'?
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"Result == 'a'"]];
答案 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
}];