1

I want to check if the attribute on a core data entity is Null or not. I tried several approaches and went through some questions on SO and surprisingly there were no answer or they didn’t work. The question here mentions all the possible ways to check the attributes value but there are no inputs that work. My app crashes when i try to check the attribute for a Null value. Any help will be appreciated. Thank you.

EDIT

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

So a case where the contact has no first name, my app will crash.

Community
  • 1
  • 1

2 Answers2

2

Please check in this way

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

Hope this help.

Gagan_iOS
  • 3,638
  • 3
  • 32
  • 51
0

first you have to call for your core data Entity like below.

 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");
           }

        }
    }
caldera.sac
  • 4,918
  • 7
  • 37
  • 69