Exclude or Negate NSComparisonPredicate

  • I'm doing an¬†NSMetadataQuery something like this:

    NSPOT = NSLikePredicateOperatorType;
    myPredicate = [NSComparisonPredicate
    predicateWithLeftExpression:[NSExpressionexpressionForKeyPath:@"*"] 
    rightExpression:[NSExpressionexpressionForConstantValue:@"somestring"]
    modifier:NSDirectPredicateModifier
    type:NSPOT
    options:options];
    [myQuery setPredicate:myPredicate];
    [myQuerystartQuery];

    But I've not seen any examples of how to search for everything that DOESN'T contain "somestring".
    I see some docs on NSCompoundPredicate and notPredicateWithSubpredicate, but does that apply to this style of predicate? If so, how would that work?

    Chris
  • You'd keep most of what you have, but just add in the negation:

    On May 21, 2012, at 11:47 AM, Chris Paveglio wrote:

    > I'm doing an NSMetadataQuery something like this:
    >
    > NSPOT = NSLikePredicateOperatorType;
    > myPredicate = [NSComparisonPredicate
    > predicateWithLeftExpression:[NSExpressionexpressionForKeyPath:@"*"]
    > rightExpression:[NSExpressionexpressionForConstantValue:@"somestring"]
    > modifier:NSDirectPredicateModifier
    > type:NSPOT
    > options:options];

    NSPredicate *notMyPredicate = [NSCompoundPredicate notPredicateWithSubpredicate:myPredicate];
    [myQuery setPredicate:notMyPredicate];

    HTH,

    Dave
  • I use this code to search for something *not* containing someString with NSMetadataquery:

    predicate = [NSPredicate predicateWithFormat:@"NOT kMDItemFSName LIKE '%@'", someString];

    As NSMetadataQuery only uses a subset of the NSPredicate syntax, I doubt whether using NSCompoundPredicate or something related will work.

    Tim

    Am 21.05.2012 um 20:47 schrieb Chris Paveglio:

    > I'm doing an NSMetadataQuery something like this:
    >
    > NSPOT = NSLikePredicateOperatorType;
    > myPredicate = [NSComparisonPredicate
    > predicateWithLeftExpression:[NSExpressionexpressionForKeyPath:@"*"]
    > rightExpression:[NSExpressionexpressionForConstantValue:@"somestring"]
    > modifier:NSDirectPredicateModifier
    > type:NSPOT
    > options:options];
    > [myQuery setPredicate:myPredicate];
    > [myQuerystartQuery];
    >
    > But I've not seen any examples of how to search for everything that DOESN'T contain "somestring".
    > I see some docs on NSCompoundPredicate and notPredicateWithSubpredicate, but does that apply to this style of predicate? If so, how would that work?
    >
    > Chris
previous month may 2012 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today