2016-04-05 44 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块代码,您可能试图获得键值表单数组不可能我 –

相关问题