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".
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.
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.
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.
Information about an error condition including a domain, a domain-specific error code, and application-specific information.
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];
How about:
NSDictionary *userInfo = [error userInfo];
NSString *error = [userInfo objectForKey:@"NSUnderlyingError"];
NSLog(@"The error is: %@", error);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With