43

I am using the NSURLRequest class in my iPhone app, and the method that calls it returns an NSString which is great for when the connection goes through ok, but the issue is I need to convert the NSError into an NSString so I can either return it back or run some if() statements on it.

Any ideas? :)

4

5 回答 5

83

-[NSError localizedDescription].

(Also, every ObjC object inherited from NSObject implements -description which returns an NSString.)

于 2010-03-07T09:26:25.790 回答
13

for folks new to objective c (me), following is example code that makes accepted answer from 'KennyTM' work ->

[self showAlertWithTitle:@"Error:" withMessage:error.localizedDescription];
于 2014-09-08T03:43:01.030 回答
7

You could try the localizedDescription method, which returns a string.

More in the docs.

于 2010-03-07T09:28:39.517 回答
5

I found that there are three main methods to NSError:

  • error (NSInteger)
  • domain (NSString)
  • userInfo (NSDictionary)
于 2010-03-07T09:28:35.787 回答
2

To get all the error details:

NSError * err;
...
[NSString stringWithFormat:@"%@", err];
于 2018-06-11T22:13:25.397 回答