Use NSPredicate to detect NOT CONTAINS

I give up. I tried every combination that I can imagine to check if a string contains another string. Here is an intuitive syntax example that describes what I want to do:

    NSPredicate* pPredicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS[c] %@)",
NSMetadataItemFSNameKey, 
[NSString stringWithFormat:@"Some String"]];

No matter how I move NOT around, use! instead, copy the brackets or delete them altogether, I always get an exception by parsing this expression.

What is wrong with this expression?

EDIT: An exception occurs when I call

[pMetadataQuery setPredicate:pPredicate];

and exception: * The application terminated due to the unmasked exception "NSInvalidArgumentException", reason: "Unknown type NSComparisonPredicate provided by NSMetadataQuery (kMDItemFSName CONTAINS [c]" Some String ") '

+5
source
2

:

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS[c] %@)",
        @"someKey",
        [NSString stringWithFormat:@"Some String"]];
NSArray *testArray =
    [NSArray arrayWithObjects:
        [NSDictionary dictionaryWithObject:@"This sure is Some String" forKey:@"someKey"],
        [NSDictionary dictionaryWithObject:@"I've nothing to say" forKey:@"someKey"],
        [NSDictionary dictionaryWithObject:@"I don't even have that key" forKey:@"someOtherKey"],
        nil];

NSArray *filteredArray = [testArray filteredArrayUsingPredicate:predicate];
NSLog(@"found %@", filteredArray);

testArray filteredArray, OS X v10.7 iOS v4.3. , - - - NSMetadataQuery. , , , , .

+10

Swift 3.0

let predicate = NSPredicate(format: "NOT (%K CONTAINS[c] %@)", "someKey", "Some String")
let testArray: [Any] = [[ "someKey" : "This sure is Some String" ], [ "someKey" : "I've nothing to say" ], [ "someOtherKey" : "I don't even have that key" ]]
let filteredArray: [Any] = testArray.filter { predicate.evaluate(with: $0) }
print("found \(filteredArray)")
+1

All Articles