gpt4 book ai didi

iphone - 断开互联网连接时如何处理 NSJSONSerialization 的崩溃

转载 作者:可可西里 更新时间:2023-11-01 05:01:16 27 4
gpt4 key购买 nike

我在我的应用程序中实现网络服务。我的方式很典型。

- (BOOL)application:(UIApplication *)application     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Web Service xxx,yyy are not true data
NSString *urlString = @"http://xxx.byethost17.com/yyy";
NSURL *url = [NSURL URLWithString:urlString];
dispatch_async(kBackGroudQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: url];
[self performSelectorOnMainThread:@selector(receiveLatest:) withObject:data waitUntilDone:YES];
});
return YES;
}

- (void)receiveLatest:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSString *Draw_539 = [json objectForKey:@"Draw_539"];
....

控制台错误信息:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

当我的 iPhone 连接到互联网时,该应用程序成功运行。但如果它断开与互联网的连接,应用程序将在 NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 上崩溃你能告诉我如何处理这个错误吗? NSError 有帮助吗?

最佳答案

错误告诉您“responseData”为零。避免异常的方法是测试“responseData”,如果它为 nil,则不调用 JSONObjectWithData。相反,您应该针对这种错误情况使用react。

关于iphone - 断开互联网连接时如何处理 NSJSONSerialization 的崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18069884/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com