Macos 使用NSPredicate按关键字过滤NSArray

Macos 使用NSPredicate按关键字过滤NSArray,macos,cocoa,nsarray,nspredicate,predicatewithformat,Macos,Cocoa,Nsarray,Nspredicate,Predicatewithformat,我有一个名为myArray的NSArray。我想过滤myArray对象,以便从该数组中排除与另一个数组keywords中的关键字对应的所有元素 这是我的伪代码: keywords = @[@"one", @"three"]; myArray = @[@"textzero", @"textone", @"texttwo", @"textthree", @"textfour"]; predicate = [NSPredicate predicateWithFormat:@"NOT (SELF CON

我有一个名为
myArray
的NSArray。我想过滤
myArray
对象,以便从该数组中排除与另一个数组
keywords
中的关键字对应的所有元素

这是我的伪代码:

keywords = @[@"one", @"three"];
myArray = @[@"textzero", @"textone", @"texttwo", @"textthree", @"textfour"];
predicate = [NSPredicate predicateWithFormat:@"NOT (SELF CONTAINS_ANY_OF[cd] %@), keywords];
myArray = [myArray filteredArrayUsingPredicate:predicate];
这就是我想通过
NSLog(@“%@”,myArray)得到的

我应该怎么做?

使用以下代码:

NSArray *keywords = @[@"one", @"three"];
NSArray *myArray = @[@"textzero", @"textone", @"texttwo", @"textthree", @"textfour"];
NSString * string = [NSString stringWithFormat:@"NOT SELF CONTAINS[c] '%@'", [keywords componentsJoinedByString:@"' AND NOT SELF CONTAINS[c] '"]];
NSPredicate* predicate = [NSPredicate predicateWithFormat:string];
NSArray* filteredData = [myArray filteredArrayUsingPredicate:predicate];
NSLog(@"Complete array %@", filteredData);
使用此代码:

NSArray *keywords = @[@"one", @"three"];
NSArray *myArray = @[@"textzero", @"textone", @"texttwo", @"textthree", @"textfour"];
NSString * string = [NSString stringWithFormat:@"NOT SELF CONTAINS[c] '%@'", [keywords componentsJoinedByString:@"' AND NOT SELF CONTAINS[c] '"]];
NSPredicate* predicate = [NSPredicate predicateWithFormat:string];
NSArray* filteredData = [myArray filteredArrayUsingPredicate:predicate];
NSLog(@"Complete array %@", filteredData);

可以使用块来过滤阵列。通常一个街区比较快

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"];
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];