Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get more useful information from NSError?

Tags:

ios

nserror

I want to get some useful information from NSError. If I print out [error userInfo], I get the following:

{
    NSFilePath = "/Users/apple/Library/Application Support/iPhone Simulator/5.1/Applications/08260B6A-4D65-48DF-ADD1-FFC8750081E8/Documents/abc";
    NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=17 \"The operation couldn\U2019t be completed. File exists\"";
}

I want to show the last line : "File exists", but how can i pick it out?

I tried:

localizedDescription
localizedFailureReason
localizedRecoverySuggestion
localizedRecoveryOptions
recoveryAttempter

Non of them show "File exists".

like image 239
kevin young Avatar asked Jun 14 '12 07:06

kevin young


People also ask

How many parts of NSError object?

Each NSError object encodes three critical pieces of information: a status code , corresponding to a particular error domain , as well as additional context provided by a userInfo dictionary.

What is made up of NSError object?

The core attributes of an NSError object are an error domain (represented by a string), a domain-specific error code and a user info dictionary containing application specific information.

What is NSError domain?

The core attributes of an NSError object—or, simply, an error object—are an error domain, a domain-specific error code, and a “user info” dictionary containing objects related to the error, most significantly description and recovery strings.

What is NS error?

Information about an error condition including a domain, a domain-specific error code, and application-specific information.


2 Answers

Finally, I follow code for perfect NSError print. Thanks @jbat100 and @Peter Warbo, I add a little bit on them code:

    NSDictionary *userInfo = [error userInfo];
    NSString *errorString = [[userInfo objectForKey:NSUnderlyingErrorKey] localizedDescription];
like image 136
kevin young Avatar answered Nov 02 '22 21:11

kevin young


How about:

NSDictionary *userInfo = [error userInfo];
NSString *error = [userInfo objectForKey:@"NSUnderlyingError"];
NSLog(@"The error is: %@", error);
like image 29
Peter Warbo Avatar answered Nov 02 '22 19:11

Peter Warbo