您可以使用该类NSCompoundPredicate
来创建复合谓词。
NSPredicate *firstNamePred = [NSPredicate predicateWithFormat:@"firstName BEGINSWITH[cd] %@", word];
NSPredicate *lastNamePred = [NSPredicate predicateWithFormat:@"lastName BEGINSWITH[cd] %@", word];
NSArray *predicates = @[firstNamePred, lastNamePred];
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates];
NSArray* resArr = [myArray filteredArrayUsingPredicate:compoundPredicate];
这是我喜欢做的一种方式。
或者你可以做...
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"firstName BEGINSWITH[cd] %@ OR lastName BEGINSWITH[cd] %@", word, word];
NSArray* resArr = [myArray filteredArrayUsingPredicate:predicate];
要么会工作。