获取coredate记录,其中布尔列值为NO或nil

时间:2015-06-01 17:31:38

标签: ios objective-c iphone core-data

我想从我的实体获取数据,其中特定列(isDelete)的值为nil or NO。我使用下面的代码:

    NSManagedObjectContext *managedObjectContext = [LIUtility sharedUtility].managedObjectContext;
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"NumberStorage" inManagedObjectContext:managedObjectContext];
    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    [fetch setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(type = %d) and isDelete != YES",type];
        [fetch setPredicate:predicate];

 allCardNumber = [managedObjectContext executeFetchRequest:fetch error:nil];

但它不起作用。

我该如何查询此请求?

2 个答案:

答案 0 :(得分:0)

这应该有效,

NSManagedObjectContext *managedObjectContext = [LIUtility sharedUtility].managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"NumberStorage" inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(type = %d) and isDelete != %@",type, @YES];
[fetch setPredicate:predicate];

allCardNumber = [managedObjectContext executeFetchRequest:fetch error:nil];

如果在模型编辑器中为布尔属性设置默认值而不是将其设置为零,那就太好了。

答案 1 :(得分:0)

Predicate Programming Guide开始,您必须明确测试nil:

  

测试空
  如果要匹配空值,除了其他比较之外,还必须包含特定测试

所以,将你的谓词修改为:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(type = %d) AND (isDelete == NO OR isDelete == nil)",type];