2016-10-04 30 views
1

我想檢查覈心數據實體上的屬性是否爲空。我嘗試了幾種方法,並通過了一些關於SO的問題,並且出人意料地沒有答案或者他們沒有工作。問題here提到了檢查屬性值的所有可能方法,但沒有任何輸入可用。當我嘗試檢查Null值的屬性時,我的應用崩潰。任何幫助將不勝感激。謝謝。檢查CoreData實體的屬性是否爲空?

編輯

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

所以當接觸沒有名字的情況下,我的應用程序會崩潰。

+0

好的。請問您可以展示您的代碼嗎? { ........代碼在這裏 }其他 { //處理問題這裏 } –

+0

這樣 如果([@ 「FIRST_NAME」 接觸valueForKey])加入@Gagan_iOS – TKutal

+2

檢查完成。在答案中添加。 –

回答

2

請這樣

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

希望這有助於檢查。

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

     } 
    } 
相關問題