应用程序崩溃当标题不存在时

 feloveyu 发布于 2023-02-13 15:24

我的应用程序显示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'

1 个回答
  • 根据错误消息,这意味着您确实有一个值@"caption"但它碰巧是一个NSNull对象.

    你有两个选择:

    1)不要NSNull在字典中放置值.

    要么

    2)更新你的if陈述:

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

    2023-02-13 15:27 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有