Returning an NSString from an NSError

女生的网名这么多〃 提交于 2019-12-20 15:44:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!