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