Home > database >  unable to set NSPredicateEditor with NOT operator and single subpredicate
unable to set NSPredicateEditor with NOT operator and single subpredicate

Time:08-19

I have an NSPredicateEditor instance named editor configured with the following row templates:

NSMutableArray *finalTemplates = [NSMutableArray arrayWithCapacity:2];
    for(NSString *keyPath in @[@"sampleName",@"comment"]) {
        NSPredicateEditorRowTemplate *template = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions:@[[NSExpression expressionForKeyPath:keyPath]]
                                                                                         rightExpressionAttributeType:NSStringAttributeType
                                                                                                             modifier:NSDirectPredicateModifier
                                                                                                            operators:@[@(NSContainsPredicateOperatorType), @(NSBeginsWithPredicateOperatorType)]
                                                                                                              options: 0];
        [finalTemplates addObject:template];
    }

NSArray *compoundTypes = @[@(NSNotPredicateType), @(NSAndPredicateType),  @(NSOrPredicateType)];
NSPredicateEditorRowTemplate *compound = [[NSPredicateEditorRowTemplate alloc] initWithCompoundTypes:compoundTypes];

editor.rowTemplates = [@[compound] arrayByAddingObjectsFromArray:finalTemplates];

I want to make it show this: enter image description here

This seems simple enough, but I can't make it work. When I try:

editor.objectValue = [NSPredicate predicateWithFormat: @"NOT sampleName CONTAINS ''"];
editor.objectValue = [NSPredicate predicateWithFormat: @"NONE sampleName CONTAINS ''"];
editor.objectValue = [NSCompoundPredicate notPredicateWithSubpredicate: [NSPredicate predicateWithFormat: @"sampleName CONTAINS ''"]];

each results in an exception:

unable to find template matching predicate (...)

When I do:

editor.objectValue = [NSPredicate predicateWithFormat: @"NOT (sampleName CONTAINS '' OR comment CONTAINS '')"];

this works, but it's not what I want.

IOW, how can I make it show the "None" row with only one other row?

Note: I have no issue making it show the "Any" or "All" row template following by "sampleName contains ...".

CodePudding user response:

NONE is NOT ( OR ). I don't think there's a format but in code it is:

[NSCompoundPredicate notPredicateWithSubpredicate:
    [NSCompoundPredicate orPredicateWithSubpredicates:
        @[[NSPredicate predicateWithFormat: @"sampleName CONTAINS ''"]]]];
  • Related