检查CoreData实体的属性是否为空?

时间:2016-10-04 10:33:58

标签: ios objective-c core-data

我想检查核心数据实体上的属性是否为Null。我尝试了几种方法并在SO上提出了一些问题,令人惊讶的是没有答案,或者它们没有用。问题here提到了检查属性值的所有可能方法,但没有可用的输入。当我尝试检查Null值的属性时,我的应用程序崩溃了。任何帮助将不胜感激。谢谢。

修改

    NSMutableAttributedString *attrStringNew = [[NSMutableAttributedString alloc] initWithString:[contact valueForKey:@"first_name"]];
    [attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica Neue" size:10] range:boldedRange];
    titleLbl.attributedText = attrStringNew;

如果联系人没有名字,我的应用程序将崩溃。

2 个答案:

答案 0 :(得分:1)

请以这种方式检查

if([contact valueForKey:@"first_name"]) 
{ ........code here }
else 
{ //Handle issue here 
}

希望得到这个帮助。

答案 1 :(得分:0)

首先,您必须拨打以下核心数据Entity

 NSManagedObjectContext *moc = self.appDelegate.managedObjectContext;
        NSFetchRequest *theRequest = [NSFetchRequest fetchRequestWithEntityName:@"Your Entity Name"];
  NSError *error = nil;
    NSArray *resultArray = [moc executeFetchRequest:theRequest error:&error];

    if (!resultArray) {
        abort();
       //check here where the array is null or not

    }
    else
    {
        //then in here you can check the objects one by one
        for (yourEntity *entity in resultArray)
        {
            NSString *myobject = entity.first_name;
            //in here your can check the each object is null or not
           if([myobject iseEqualToString:@""])
           {
               NSLog(@"the object is null");
           }

        }
    }
相关问题