如果没有互联网,请阻止应用崩溃

时间:2014-05-23 09:12:40

标签: ios objective-c

我的应用程序在没有互联网时崩溃。这是因为

NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData
                                                               options:NSJSONReadingMutableContainers error:&error];

返回错误'data parameter is nil'

当我使用SDWebImage缓存jsonData时,如何防止此崩溃。  即使没有互联网,它仍然应该存在!

2 个答案:

答案 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);
    }
相关问题