【发布时间】:2011-08-29 04:21:52
【问题描述】:
iPhone 客户端应用程序在收到 NULL 作为 jsonData 参数时崩溃。使用第三方JSONKit库,代码如下:
- (id)objectWithData:(NSData *)jsonData error:(NSError **)error
{
if(jsonData == NULL) { [NSException raise:NSInvalidArgumentException format:@"The jsonData argument is NULL."]; }
return([self objectWithUTF8String:(const unsigned char *)[jsonData bytes] length:[jsonData length] error:error]);
}
JSONKit 文档说:
重要提示:如果 string 为 NULL,objectWithUTF8String: 和 mutableObjectWithUTF8String: 将引发 NSInvalidArgumentException。
问题:我应该如何处理这种情况,以便 iPhone 应用程序不会在这种情况下崩溃?不是找理论上的异常处理代码,而是提示应用程序一般如何处理 jsonData == NULL 的情况?
【问题讨论】:
-
通过确保数据不为空????
-
很好的答案,除了我无法访问服务器:)
-
但是您不需要访问服务器来确定字符串是否为空...