核心数据NSPredicate检查BOOL值 [英] Core Data NSPredicate checking for BOOL value

查看:746
本文介绍了核心数据NSPredicate检查BOOL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个问题,从db从ie的所有数据参数为TRUE。



我使用 NSPredicate 下面是一个示例代码

  NSManagedObjectContext * context = managedObjectContext_; 

if(!context){
//处理错误。
NSLog(@ERROR CONTEXT IS NIL);
}

NSEntityDescription * entity = [NSEntityDescription entityForName:@tblcontentinManagedObjectContext:managedObjectContext_];

NSFetchRequest * request = [[NSFetchRequest alloc] init];

NSPredicate * predicate = [NSPredicate predicateWithFormat:@bookmarked == YES];

[request setPredicate:predicate];

我尝试将predicatewithformat设置为几乎所有内容,但它仍然不提取包含 YES 值。



我还试过了(@bookmarked ==%d,YES) / code>但没有运气。我不想得到整个数组,然后通过 if(object.bookmarked == YES) ..... blabla手动过滤它。



非常感谢。

解决方案

基于Apple Document 这里,我们可以使用以下两种方法来比较布尔值:

  NSPredicate * newPredicate = [NSPredicate predicateWithFormat :@anAttribute ==%@,[NSNumber numberWithBool:aBool]]; 
NSPredicate * testForTrue = [NSPredicate predicateWithFormat:@anAttribute == YES];但是,上面的谓词不能得到空的 anAttribute

code>。要处理空属性,您需要根据Apple文档 here

  predicate = [NSPredicate predicateWithFormat:@firstName = nil]; //它在文档

  predicate = [NSPredicate predicateWithFormat:@firstName == nil]; // ==和=在这里可以互换。


I am currently having an issue pulling all data from db whereby i.e 1 parameter is TRUE.

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];

I tried setting predicatewithformat to almost everything but it still does not pull out bookmarks which have a YES value.

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.

Many thanks.

解决方案

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"];

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

or

predicate = [NSPredicate predicateWithFormat:@"firstName == nil"]; // == and = are interchangeable here

这篇关于核心数据NSPredicate检查BOOL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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