本文介绍了NSPredicate格式字符串不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我想检查一个记录是否已经存在,所以我知道是否创建它或更新它。但我遇到了一个问题。问题是当我使用这个:
In my code, I want to check and see if a record already exists, so I know whether to create it or update it. But I ran into a problem. The problem is when I use this:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"%@ == %@", ATTRIBUTE_ID, idNumber];
[request setPredicate:pred];
这不起作用。它总是声称没有找到结果。然而,它工作正常,当我重写如下:
This doesn't work. It always claims no results were found. However, it works just fine when I rewrite it like so:
NSExpression *lhs = [NSExpression expressionForKeyPath:ATTRIBUTE_ID];
NSExpression *rhs = [NSExpression expressionForConstantValue:idNumber];
NSPredicate *pred = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSEqualToPredicateOperatorType
options:0];
[request setPredicate:pred];
我在格式字符串中丢失或做错了什么?
What am I missing or doing incorrectly in the format string?
ATTRIBUTE_ID
是一个键,所以你应该使用%K
Your
ATTRIBUTE_ID
is a key, so you should use %K
in the format string for that part of it.
格式字符串看起来像这样(注释中指出的大写K):
The format string would look like this (upper case K as pointed out in the comments):
[NSPredicate predicateWithFormat:@"%K == %@", ATTRIBUTE_ID, idNumber];
这篇关于NSPredicate格式字符串不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!