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? :)
-[NSError localizedDescription]
.
(Also, every ObjC object inherited from NSObject implements -description
which returns an NSString.)
for folks new to objective c (me), following is example code that makes accepted answer from 'KennyTM' work ->
[self showAlertWithTitle:@"Error:" withMessage:error.localizedDescription];
You could try the localizedDescription
method, which returns a string.
More in the docs.
I found that there are three main methods to NSError
:
- error (NSInteger)
- domain (NSString)
- userInfo (NSDictionary)
To get all the error details:
NSError * err;
...
[NSString stringWithFormat:@"%@", err];
来源:https://stackoverflow.com/questions/2395740/returning-an-nsstring-from-an-nserror