NSPredicate与boolean不是一个布尔值比较 [英] NSPredicate with boolean is not comparing a boolean to NO

查看:350
本文介绍了NSPredicate与boolean不是一个布尔值比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CoreData,我的实体有一个名为isReward的布尔属性。



我试图获取实体并过滤出所有结果,其中isReward ==是。



isReward!= YES 无法使用。



isReward == NO 无法使用。



NSPredicate predicateWithFormat:isReward!=%@,[NSNumber numberWithBool:YES]] / p>

isReward == Nil 有效。



解决方案

我猜你会想要:

  [NSPredicate predicateWithFormat:@isReward = NO OR isReward = nil] 

(根据Apple文档这里,直接比较 NO 应该有效。)



,当与数据术语中的 null / nil 比较时, null == false 计算为 false null!= false 计算 false 。与 null 的比较通常评估为 false ,除非测试 IS NULL



我还说,一般来说,索引一般会在搜索平等而不是不等式时给出更好的结果。在你的情况下,这是相当简单的重构(虽然我不知道一般对核心数据将是重要的)。



此外,如果你想总是 NO (并跳过 nil ),您需要明确地将默认值设置为 NO



但是,您可能有一些语义值在它$ nil 之前,直到它被你的应用程序明确设置。这真的取决于你想做什么。


I'm using CoreData and my entity has a boolean property called "isReward".

I'm trying to fetch the entity and filter out all results where isReward == YES.

isReward != YES does not work.

isReward == NO does not work.

NSPredicate predicateWithFormat:"isReward != %@", [NSNumber numberWithBool:YES]] does not work

isReward == Nil works.

What gives?

解决方案

I'm guessing you'll want:

[NSPredicate predicateWithFormat:@"isReward = NO OR isReward = nil"]

(Per the Apple docs here, direct comparison to NO should be valid.)

In general, when comparing to null/nil in data terms, null == false evaluates to false and null != false evaluates false. Comparison to null generally evaluates to false, except when testing for IS NULL.

I would also say that indexing in general gives you better results when you search for equality, not inequality. This is fairly simple to refactor to in your case (though I'm not sure that it will matter to Core Data in general).

Also, if you would like it to always be YES or NO (and skip the check for nil), you need to explicitly set the default to NO.

However, you may have some semantic value in leaving it nil until it is explicitly set by your application. This is really up to what you're trying to do.

这篇关于NSPredicate与boolean不是一个布尔值比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆