2016-04-05 49 views
0

我遇到了最奇怪的問題。我使用下面的代碼來顯示登錄用戶的個人資料照片(我正在從Drupal數據庫的字段中檢索圖像)。即使使用if/else語句,空值也會導致崩潰?

.M

//USER PHOTO 

    NSDictionary *user = [[DIOSSession sharedSession] user]; 
    NSString *secondLink = user[@"field_photo_path"][@"und"][0][@"safe_value"]; 

    NSLog(@"This is second link %@", secondLink); 

if([secondLink length]>0) { 


    NSString *ImageURL = secondLink; 
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]]; 
// self.imageView.image = [UIImage imageWithData:imageData]; 
    self.imageView.image = [[UIImage alloc] initWithData:imageData]; 

    } else { 

    NSString *ImageURL = @"http://url.com/default.png"; 
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]]; 
    self.imageView.image = [UIImage imageWithData:imageData]; 
} 

當然,如果是secondlink爲空(或空的,例如沒有照片已上傳),我收到以下錯誤:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArray0 objectForKeyedSubscript:]: unrecognized selector sent to instance 0x14f602fe0'

我想我解決這個問題與if/else語句,但顯然不是。我怎樣才能解決這個問題?

乾杯!

+1

哪條線是崩潰?基於這個消息,它看起來像是在'NSString * secondLink = ...'行和你使用的一箇中間值,就像它是一個字典實際上是一個數組。 – dan

+0

@dan NSString * secondLink = user [@「field_photo_path」] [@「und」] [0] [@「safe_value」]; - 這是崩潰的行,但只有當field_photo_path爲空時才行。當它是空的時候,我需要它不會崩潰。 – Brittany

+0

它不會崩潰,如果它是空的東西那裏包含你不期望的東西。我建議你使用變量來訪問所有的東西,這樣你就有機會調試它。 – gnasher729

回答

0
NSString *secondLink = user[@"field_photo_path"][@"und"][0][@"safe_value"]; 

您需要先驗證用戶字典是否有效,然後再讀取它。

if (user[@"field_photo_path"]) { 

    // you can look for [@"und"] here 
    // providing it is a dictionary and not an array 
    if([user[@"field_photo_path"] isKindOfClass:[NSDictionary class]]){ 
     if (user[@"field_photo_path"][@"und"]) { 
      // etc 
     } 
    } 
} 

您可以通過建模User類爲NSObject子類減少這種嵌套的代碼,幷包括在類方法來安全地分析這些數據,讓您只在一個地方寫這個驗證代碼。

編輯 正如評論中提到:

如果數據在user[@"field_photo_path"][@"und"]NSDictionary,而不是一個NSArray你不能[0]訪問它,並會得到你的崩潰。您有一個索引(array[0]array[1]等)和NSDictionary用鑰匙(dict[@"name"]dict[@"und"]等)

+0

好吧,我很困惑哈哈。對不起,我是一個新手。我不明白這段代碼如何保持錯誤發生?這些字段返回正常,錯誤只發生在數據不存在時? – Brittany

+0

是的訪問不存在或錯誤的類型對象的對象時發生錯誤請嘗試我的答案我試圖更正您的代碼,它可能會解決問題 –

+0

@TarunSeerayour代碼是不足以防止崩潰。如果用戶[@「field_photo_path」] [@「und」]中的數據是NSDictionary而不是NSArray,則無法通過[0]訪問它,並且會導致崩潰。你用一個索引(array [0],array [1]等等)訪問NSArray,並用鍵(字典[@「name」] dict [@「und」]等)訪問NSDictionary。 – davbryn

0

解決方案是將檢查你的關鍵的

//用戶提供的照片訪問NSArray's

NSDictionary *user = [[DIOSSession sharedSession] user]; 
if (user[@"field_photo_path"]) { 


    if ([user[@"field_photo_path"] isKindOfClass:[NSDictionary class]]) { 

     if (user[@"field_photo_path"][@"und"]) { 
      if ([user[@"field_photo_path"][@"und"]isKindOfClass:[NSArray class]]) { 

       if ([user[@"field_photo_path"][@"und"] count]>0) { 

        if (user[@"field_photo_path"][@"und"][0][@"safe_value"]) { 

         if ([user[@"field_photo_path"][@"und"][0][@"safe_value"] isKindOfClass:[NSDictionary class]]){ 

          NSString *secondLink = user[@"field_photo_path"][@"und"][0][@"safe_value"]; 

          NSLog(@"This is second link %@", secondLink); 

          if([secondLink length]>0) { 


           NSString *ImageURL = secondLink; 
           NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]]; 
           // self.imageView.image = [UIImage imageWithData:imageData]; 
           self.imageView.image = [[UIImage alloc] initWithData:imageData]; 

          } else { 

           NSString *ImageURL = @"http://url.com/default.png"; 
           NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]]; 
           self.imageView.image = [UIImage imageWithData:imageData]; 
          } 
         } 


        } 
       } 

      } 
     } 

    } 
} 

您還可以使用try catch塊來避免崩潰

@try { 
    //USER PHOTO 

    NSDictionary *user = [[DIOSSession sharedSession] user]; 
    NSString *secondLink = user[@"field_photo_path"][@"und"][0][@"safe_value"]; 

    NSLog(@"This is second link %@", secondLink); 

    if([secondLink length]>0) { 


     NSString *ImageURL = secondLink; 
     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]]; 
     // self.imageView.image = [UIImage imageWithData:imageData]; 
     self.imageView.image = [[UIImage alloc] initWithData:imageData]; 

    } else { 

     NSString *ImageURL = @"http://url.com/default.png"; 
     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]]; 
     self.imageView.image = [UIImage imageWithData:imageData]; 
    } 
} 
@catch (NSException *exception) { 
    //Handle expecption 
} 
@finally { 

} 
+0

嘗試了你的第一塊代碼,錯誤仍然發生:/ – Brittany

+0

雖然你的捕獲工程令人驚訝。 ..大聲笑:) – Brittany

+0

沒問題,你可以打印你的NSDictionary *用戶和共享的價值,以便我們可以檢查什麼數據正是在那裏,也嘗試@try塊代碼,您可能試圖獲得鍵值表單數組不可能我 –

相關問題