1

我正在尝试从 json 响应中过滤产品列表。对于左侧表达式“状态”右侧,我需要一个下拉菜单。所以我用了正确的表达方式。在使用 nspredicate 过滤此 json 时,由于空间原因,它无法正常工作。请帮助我实现这一目标。

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

1 回答 1

3

我有点猜测(因为我没有建立一个测试项目来验证这一点),但“右手边”是常量值而不是关键路径。所以你应该更换

 [NSExpression expressionForKeyPath:@"Awaiting inputs"]

经过

[NSExpression expressionForConstantValue:@"Awaiting inputs"]

在 中NSArray *right=@[ ... ],对于其他可能的值也是如此。

于 2013-12-26T16:17:21.920 回答