34

I am having the method in my view controller as shown below:

- (void)parser:(PaymentTermsLibxmlParser *)parser encounteredError:(NSError *)error
{
    NSLog("error occured");
}

Here I need to show the Actual error message in the NSError in my alert can any one suggest how to get it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Monish Kumar
  • 2,788
  • 4
  • 38
  • 54

4 Answers4

46

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.)

jtbandes
  • 115,675
  • 35
  • 233
  • 266
37

To get error message only, use:

NSString *msg = [error localizedDescription];

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

NSLog(@"Error: %@", error);
Top-Master
  • 7,611
  • 5
  • 39
  • 71
mkodamati
  • 376
  • 3
  • 7
5

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]];
Alex Zavatone
  • 4,106
  • 36
  • 54
1

User error.userInfo, it returns dictionary ex:

 NSLog(@"%@",error.userInfo);
{
code = 101;
error = "invalid login parameters";
originalError = "Error Domain=NSURLErrorDomain Code=-1011 \"The operation        couldn\U2019t be completed. (NSURLErrorDomain error -1011.)\"";
temporary = 0;
}
Baljeet Singh
  • 453
  • 5
  • 15