我正在尝试使用一种常见的方法来获取NSPredicates,这会导致解析错误。但是,如果我编写内联这样的东西:
NSPredicate *predicateTax_added = [NSPredicate predicateWithFormat:@"tax_added = %@",@"YES"];
它有效。
然而,以下不起作用:
NSPredicate *predicateTax_added = [self createPredicateWithFormateWhereKey:@"tax_added" operator:NSEqualToPredicateOperatorType value:@"YES"];
-(NSPredicate *)createPredicateWithFormateWhereKey:(NSString *)key operator:(NSPredicateOperatorType *)operatorType value:(NSString *)value
{
return [NSPredicate predicateWithFormat:@"%@ %@ %@",key, operatorType, value];
}
任何建议,谢谢。
答案 0 :(得分:0)
有一个类似的谓词方法,predicateWithSubstitutionVariables
在此处查看更多信息:
我认为predicateWithFormat
不能像你一样使用。
答案 1 :(得分:0)
行中有两个问题
[NSPredicate predicateWithFormat:@"%@ %@ %@",key, operatorType, value]
%K
格式代替%@
。predicateWithFormat
中的运算符。要从变量键,值和运算符构建谓词,您必须构造 谓词编程:
-(NSPredicate *)createPredicateWithFormateWhereKey:(NSString *)key operator:(NSPredicateOperatorType)operatorType value:(NSString *)value
{
NSExpression *left = [NSExpression expressionForKeyPath:key];
NSExpression *right = [NSExpression expressionForConstantValue:value];
NSPredicate *predicate = [NSComparisonPredicate
predicateWithLeftExpression:left
rightExpression:right
modifier:0
type:operatorType
options:0];
return predicate;
}
另请注意NSPredicateOperatorType
是标量而不是对象,并且必须
因此不能作为指针传递。