我有一些代碼使用Dribbble API來返回一個Shot,但是,如果JSON中的任何數據是空的,應用崩潰,我試圖實現並捕獲,如果它是null繼續前進,但這似乎不起作用。請有人建議?JSON返回空崩潰應用
非常感謝
詹姆斯
self.JSONString = [NSString stringWithFormat:@"http://api.dribbble.com/shots/1970521"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.JSONString]];
__block NSDictionary *json;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data != nil) {
json = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
NSLog(@"Async JSON: %@", json);
}
else {
return;
}
}];
在處理'null'的'json'後,您的應用必須崩潰。解決方法是[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];無法解析您的數據,原因可能是傳遞給它的非UTF數據。 – iphonic 2015-03-13 10:31:59
/questions/5716942/touchjson-dealing-with-nsnull – Wain 2015-03-13 10:31:59
您應該將XCode調試器附加到您的應用程序併發布崩潰回溯,否則很難爲您提供幫助。拋出異常嗎?它是什麼樣的崩潰? – Bensge 2015-03-13 10:36:10