objective-ccocoanspredicatenspredicateeditor

How to resolve space problems while using nspredicate in objective c?


I'm trying to filter a list of products from the json response. For the left side expression 'Status' right side i need a drop down. so i used right expression. While filtering this json using nspredicate its not working because of the space. Please help me to reach this.

NSArray *Mainarray;// Suppose this contains the response as "status"="Checked In" for 1 product and for other product "status"="Lost/Missing"

NSArray *left=@[[NSExpression expressionForKeyPath:@"status"]];

NSArray *right=@[[NSExpression expressionForKeyPath:@"Awaiting inputs"],[NSExpression expressionForKeyPath:@"Checked In"],[NSExpression expressionForKeyPath:@"Checked out"],[NSExpression expressionForKeyPath:@"Lost/Missing"],[NSExpression expressionForKeyPath:@"xxx Testing/Primary System"],[NSExpression expressionForKeyPath:@"Sold To"],[NSExpression expressionForKeyPath:@"closed"],[NSExpression expressionForKeyPath:@"Used for Parts"]];

NSArray *operators= @[@(NSEqualToPredicateOperatorType),@(NSNotEqualToPredicateOperatorType)];

NSArray *compoundTypes = @[@(NSAndPredicateType),@(NSOrPredicateType),@(NSNotPredicateType)];

NSPredicateEditorRowTemplate *template;

template=[[NSPredicateEditorRowTemplate alloc]initWithLeftExpressions:left rightExpressions:right modifier:NSDirectPredicateModifier operators:operators options:(NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption)];

compoundtemp = [[NSPredicateEditorRowTemplate alloc] initWithCompoundTypes:compoundTypes];

[predicateEditor setRowTemplates:@[template,compoundtemp]];


NSPredicate *predicate=[predicateEditor predicate];

NSArray *resultArray=[Mainarray filteredArrayUsingPredicate:predicate];

Solution

  • I am a little bit guessing (because I did not build a test project to verify this), but the "right hand sides" are constant values and not key paths. So you should replace

     [NSExpression expressionForKeyPath:@"Awaiting inputs"]
    

    by

    [NSExpression expressionForConstantValue:@"Awaiting inputs"]
    

    in NSArray *right=@[ ... ], and similarly for the other possible values.