iOS App终止于connectionDidFinishLoading

时间:2017-07-31 08:21:23

标签: ios

iOS应用程序在以下代码行的connectionDidFinishLoading中终止于Xcode模拟器。

NSMutableDictionary *dict = [parser objectWithString:[[NSString alloc] initWithData:dataForConnection encoding:NSUTF8StringEncoding] error:nil];

遵循此功能的更多代码

- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
NSMutableData *dataForConnection = [self dataForConnection:(URLConnection*)connection];
NSInteger statusCode=[((URLConnection*)connection).response statusCode];
NSString *tag=((URLConnection*)connection).tagKey;

[self removeReceivedDataHandle:tag];

if (statusCode != 200 && statusCode!=204 && statusCode!=405){
    [reportActivityIndicator stopAnimating];
    [transactionsActivityIndicator stopAnimating];
    [swipeHQCheckout showMessage:PHRASE_ServerCommunicationError];
    return;
}

if ([reports count] == 0) {
    [self removeReceivedDataHandle];
    [transactionsActivityIndicator stopAnimating];
    return;
}

SBJsonParser *parser = [[SBJsonParser alloc] init];
[reports removeObject:tag];

if ([tag isEqualToString:API_TransactionReport] ||
    [tag isEqualToString:API_FetchTransactions]) {
    NSMutableDictionary *dict = [parser objectWithString:[[NSString alloc] initWithData:dataForConnection encoding:NSUTF8StringEncoding] error:nil]; // here issue

    NSString *response_code=[dict objectForKey:@"response_code"]; 
// more code down here
}

可能是什么问题,感谢您的建议。

2 个答案:

答案 0 :(得分:0)

您的NSMutableData dataForConnection似乎没有了,您正在使用nil值初始化NSMutableDictionary

为避免崩溃:

if (dataForConnection != nil){
     NSMutableDictionary *dict = [parser objectWithString:[[NSString alloc] initWithData:dataForConnection encoding:NSUTF8StringEncoding] error:nil]; 
}else{
    NSLog(@"NO Data");
} 

答案 1 :(得分:0)

您应该在方法开头添加检查以遵循最佳做法,例如

if (dataForConnection == nil){
      NSLog(@"NO Data");
      return;
}else{
    //Do whatever you want to do
}