0

I have this code after a remote call is made and

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {   
         NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
         NSString *responseError = [[NSString alloc] initWithData:error encoding:NSUTF8StringEncoding] ;
         ...

And on the last line sometimes the code crashes. I can not reproduce the crash, but one guess I have is that if the error object is nil then the crash happens, am I right? Should I check if error is not nil first?

This is the error from crashalytics:

Reason:
-[NSURLError bytes]: unrecognized selector sent to instance 0x22b215d0

which does not support my guess of why the error happens. Any idea why this crash happens?

Thanks!

4

1 回答 1

2

您不能将NSError对象传递给initWithData:方法(该方法需要一个NSData对象。很可能您会收到编译器警告。永远不要忽略编译器警告。您的代码应该编译干净。

要获取错误消息,请使用正确的NSError方法,例如localizedDescription.

NSString *responseError = [error localizedDescription];
于 2012-11-18T21:39:15.680 回答