我的应用程序在没有互联网时崩溃。这是因为
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers error:&error];
返回错误'data parameter is nil'
。
当我使用SDWebImage缓存jsonData
时,如何防止此崩溃。
即使没有互联网,它仍然应该存在!
答案 0 :(得分:7)
如果数据不是nil,您可以确保执行代码:
if (jsonData) {
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers error:&error];
}
答案 1 :(得分:0)
不能只处理nil,也没有正确的json格式,你可以获得错误消息以便进一步
@try {
responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
}
@catch (NSException *exception) {
NSLog(@"exception %@",exception);
}