开发者

How to get the NSError message in iOS?

I am having the me开发者_开发百科thod 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.


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]];


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜