核心数据 NSPredicate 检查 BOOL 值 [英] Core Data NSPredicate checking for BOOL value
问题描述
我目前在从数据库中提取所有数据时遇到问题,即 1 个参数为 TRUE.
I am currently having an issue pulling all data from db whereby i.e 1 parameter is TRUE.
我正在使用 NSPredicate
,下面是示例代码
I am using NSPredicate
and below is a sample code
NSManagedObjectContext *context = managedObjectContext_;
if (!context) {
// Handle the error.
NSLog(@"ERROR CONTEXT IS NIL");
}
NSEntityDescription *entity = [NSEntityDescription entityForName:@"tblcontent" inManagedObjectContext:managedObjectContext_];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bookmarked == YES"];
[request setPredicate:predicate];
我尝试将 predicatewithformat 设置为几乎所有内容,但它仍然没有拉出具有 YES
值的书签.
I tried setting predicatewithformat to almost everything but it still does not pull out bookmarks which have a YES
value.
我什至尝试过 (@"bookmarked == %d",YES)
但不走运.我不想获取整个数组,然后通过执行 if(object.bookmarked == YES)
.....blabla.
I even tried (@"bookmarked == %d",YES)
but with not luck. I don't want to have to get the whole array and then filter it manually by doing if(object.bookmarked == YES)
.....blabla.
我会非常感谢您的帮助.
I will really appreciate some help.
非常感谢.
推荐答案
基于 Apple Document 这里,我们可以使用以下两种方法来比较Boolean:
Based on Apple Document Here, we can use the following two methods to compare Boolean:
NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@"anAttribute == %@",[NSNumber numberWithBool:aBool]];
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"anAttribute == YES"];
但是,上面的谓词不能把anAttribute
为空的谓词取出来.处理空属性需要根据苹果文档这里:
However, the above predicate cannot get out the ones with empty anAttribute
. To deal with an empty attribute, you need the following method according to Apple document here:
predicate = [NSPredicate predicateWithFormat:@"firstName = nil"]; // it's in the document
或
predicate = [NSPredicate predicateWithFormat:@"firstName == nil"]; // == and = are interchangeable here
这篇关于核心数据 NSPredicate 检查 BOOL 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!