Search code examples
iosxcodeerror-handlingnsurl

NSURL Error code extraction


I try to do my NSURL error-handling and have a little problem. When i'm getting an error i want to handle it different for different error-codes.

In my case i want to check if the error that i'm getting is the one with errorcode 1005.

This is what I get when I print as follows:

NSLog(@"%@",error);

Error Domain=NSURLErrorDomain Code=-1005 "The operation couldn’t be completed. (NSURLErrorDomain error -1005.)" UserInfo=0x1700ff000 {NSErrorFailingURLStringKey=MyURL, NSErrorFailingURLKey=MyURL, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSURLSessionDownloadTaskResumeData=<CFData 0x17025a580 [0x1936b2c80]>{length = 3640, capacity = 4096, bytes = 0x3c3f786d6c2076657273696f6e3d2231 ... 2f706c6973743e0a}, NSUnderlyingError=0x17025cf80 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1005.)"}

and this is what I want to extract from it: -1005

I already searched for it and found a solution which doesn't work. (I get EXC_BAD_ACCESS) NSLog(@"%@", [error code]);

Afterwards I want to compare it similar to:

if(error.code == -1005){
//handle error
}

Solution

  • You can't use:

    NSLog(@"%@", [error code]);
    

    Because code is NSInteger. Use:

    NSLog(@"%d", [error code]);
    

    Refer: NSError Class Reference for more details