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:
(Per the Apple docs here, direct comparison to
NOshould be valid.)In general, when comparing to
null/nilin data terms,null == falseevaluates tofalseandnull != falseevaluatesfalse. Comparison tonullgenerally evaluates tofalse, except when testing forIS 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
YESorNO(and skip the check fornil), you need to explicitly set the default toNO.However, you may have some semantic value in leaving it
niluntil it is explicitly set by your application. This is really up to what you’re trying to do.