0

我正在创建这个应用程序,但是当 NSDictionary 中的给定条目没有值时,我不知道如何处理该错误。这是我目前拥有的代码:

   NSDictionary *entry = [self entries][indexPath.row];
    NSDictionary *text = [self entries][indexPath.row];
    NSString *user = entry[@"user"][@"full_name"];
    NSString *caption = text[@"caption"][@"text"];

    if (caption != nil && entry != [NSNull null] && text != nil && caption != [NSNull null]) {
        RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self title:user message:caption];
        [modal show];
    }

这是我在没有任何标题的情况下点击单元格时收到的错误响应:

2013-08-08 02:36:57.871 Floadt[5566:c07] -[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance 0x310b678
2013-08-08 02:36:57.872 Floadt[5566:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance 0x310b678'
*** First throw call stack:
(0x2fda012 0x260be7e 0x30654bd 0x2fc9bbc 0x2fc994e 0x5606b 0x1c7a42f 0x1c8c182 0x1c8c394 0x261f705 0x188693c 0x18869ac 0x261f705 0x188693c 0x18869ac 0x1a401d3 0x2fa2afe 0x2fa2a3d 0x2f807c2 0x2f7ff44 0x2f7fe1b 0x2bb77e3 0x2bb7668 0x1778ffc 0x2d2d 0x2c55)
libc++abi.dylib: terminate called throwing an exception
4

2 回答 2

0

不清楚为什么entrytext是一回事。这可能是一个导致您出现问题的错字:

NSDictionary *entry = [self entries][indexPath.row];
NSDictionary *text = [self entries][indexPath.row];

然后,你应该真正使用isEqual你的相等检查(即使只是为了防止你养成坏习惯)并检查获得的值(你目前没有检查user):

if (![user isEqual:[NSNull null]] && ![caption isEqual:[NSNull null]]) {

您当前的检查大多是多余的 - 您需要在使用它们之前检查它们,而不是之后。检查entry并且text必须提前完成。然后检查usercaption

于 2013-08-08T06:54:32.613 回答
0

为什么你使用两个NSDictionary虽然它们存储相同的值[self entries][indexPath.row]?

更改您的检查如下

if (!caption && ([entry isKindOfClass:[NSDictionary class]]) && ([text isKindOfClass:[NSDictionary class]]))
于 2013-08-08T07:01:39.320 回答