Iphone NSDictionary错误处理
我正在创建这个应用程序,但是当NSDictionary中的给定条目没有值时,我不知道如何处理错误。以下是我目前拥有的代码:Iphone NSDictionary错误处理,iphone,ios,objective-c,error-handling,nserror,Iphone,Ios,Objective C,Error Handling,Nserror,我正在创建这个应用程序,但是当NSDictionary中的给定条目没有值时,我不知道如何处理错误。以下是我目前拥有的代码: NSDictionary *entry = [self entries][indexPath.row]; NSDictionary *text = [self entries][indexPath.row]; NSString *user = entry[@"user"][@"full_name"]; NSString *caption = t
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
不清楚为什么
条目
和文本
是同一件事。这可能是导致您出现问题的打字错误:
NSDictionary *entry = [self entries][indexPath.row];
NSDictionary *text = [self entries][indexPath.row];
然后,您应该真正使用isEqual
进行平等性检查(即使只是为了防止您养成坏习惯),并检查获得的值(您当前没有检查user
):
您当前的检查大多是多余的-您需要在使用之前检查,而不是之后。必须提前检查
条目
和文本
。然后检查user
和caption
为什么使用两个NSDictionary
,尽管它们存储相同的值[self entries][indexPath.row]?
更改您的支票如下
if (!caption && ([entry isKindOfClass:[NSDictionary class]]) && ([text isKindOfClass:[NSDictionary class]]))
你能分享一下你字典的结构吗?多余的。。。在错误检查中,一切都是多余的,Objective-C不是Java,您不必检查
nil
。但是,您忽略了其本质:if([entry iskindof class:[NSDictionary class]])
和if([text iskindof class:[NSDictionary class]])
上面的代码中的那些行将放在哪里@H2CO3@prnk28第一个在第一行之前,第二个在第二行之前。
if (!caption && ([entry isKindOfClass:[NSDictionary class]]) && ([text isKindOfClass:[NSDictionary class]]))