您可以使用块来过滤数组。通常一个块更快。
keywords = @[@"one", @"three"];
myArray = @[@"textzero", @"textone", @"texttwo", @"textthree", @"textfour"];
predicate = [NSPredicate predicateWithBlock:^(NSString *evaluatedObject, NSDictionary<NSString *,id> *bindings){
for (NSString *key in keywords)
if ([evaluatedObject rangeOfString:key options:NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch].location != NSNotFound)
return NO;
return YES;
}];
myArray = [myArray filteredArrayUsingPredicate:predicate];
或者
keywords = @[@"one", @"three"];
myArray = @[@"textzero", @"textone", @"texttwo", @"textthree", @"textfour"];
NSIndexSet *indices = [myArray indexesOfObjectsPassingTest:^(NSString *obj, NSUInteger idx, BOOL *stop){
for (NSString *key in keywords)
if ([obj rangeOfString:key options:NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch].location != NSNotFound)
return NO;
return YES;
}];
myArray = [myArray objectsAtIndexes:indices];