How to get the NSError message in iOS? How to get the NSError message in iOS? objective-c objective-c

How to get the NSError message in iOS?


Normally you'll want to use [error localizedDescription] to get the text to show to the user.

Read the NSError documentation for more options.

For simple logging when developing, you can do NSLog(@"Error: %@", error). (That will give you 'localizedDescription' and everything else on your log in Xcode.)


To get error message only, use:

NSString *msg = [error localizedDescription];

But for logging more details, use %@ format, like:

NSLog(@"Error: %@", error);


To add to the current answers, you can get the failure message and the failure reason. To do that, you can do this when presented with an NSError:

NSString *message = [NSString stringWithFormat:@"%s\n%@\n%@", __PRETTY_FUNCTION__, displayRegion, [error localizedDescription], [error localizedFailureReason]];

This will create a 3 line string with the name of the method where the error occurred, the description of the error and a sentence explaining the error.

If more info is be supplied in the NSError, you can get the localizedRecoverySuggestion as well and add that to the message like so:

NSString *message = [NSString stringWithFormat:@"%s\n%@\n%@\n%@", __PRETTY_FUNCTION__, displayRegion, [error localizedDescription], [error localizedFailureReason], [error localizedRecoverySuggestion]];