【问题标题】:App Crashing when Caption isn't there [closed]没有字幕时应用程序崩溃[关闭]
【发布时间】:2013-12-01 22:39:46
【问题描述】:

我的应用程序会显示 Instagram 图片的标题,并且每次没有标题时都会崩溃。我将如何防止这种情况发生?这是我正在使用的代码:

if (entry[@"caption"][@"text"]) {
    NSString *caption = entry[@"caption"][@"text"];
    UILabel *instagramCaptionLabel = (UILabel *)[cell viewWithTag:103];
    [instagramCaptionLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:12.0]];
    [instagramCaptionLabel setText:caption];
}

这是错误:

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

【问题讨论】:

  • 每当您发布有关错误的问题时,您必须包括准确和完整的错误,并指出导致错误的确切代码行。

标签: ios objective-c cocoa-touch nsdictionary instagram


【解决方案1】:

根据错误消息,这意味着您确实有 @"caption" 的值,但它恰好是 NSNull 对象。

你有两个选择:

1) 不要将NSNull 值放入字典中。

2) 更新您的if 声明:

if (entry[@"caption"] != [NSNull null] && entry[@"caption"][@"text"] != [NSNull null])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多