[英]IOS/Objective-C: NSPredicate and NULL values in core data fetch
I am trying to fetch items that do not have a value of 1 in an NSNumber attribute. 我正在尝试获取NSNumber属性中值不为1的项目。 Some of the items I want to fetch have never had the attribute set at all and therefore have a NSNull value.
我要获取的某些项目根本没有设置属性,因此具有NSNull值。 How can I fetch items that are not 1 but may be NSNull?
如何获取不是1但可能是NSNull的项目?
The attribute is an NSNumber
and I am also a bit confused about whether I should be searching on 1
or @1
. 该属性是
NSNumber
,对于是否应该在1
或@1
上进行搜索,我也有些困惑。
The following code is excluding NSNull values when I want to include them: 以下代码在我要包括NSNull值时将它们排除在外:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"&hasserverid!=1"];
Thanks for any suggestions. 感谢您的任何建议。
You can explicitly test for both cases with an OR: 您可以使用OR明确测试这两种情况:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hasserverid != YES OR hasserverid == NULL"];
By the name of your property, I assume when you say 1 you're actually referring to the value true. 用您的属性名称,我假设当您说1时,您实际上是在说值true。 Using YES in the predicate format instead of 1 makes your predicate more legible.
以谓词格式而不是1使用YES会使谓词更易读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.