Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an NSString from an NSError

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? :)

like image 391
tarnfeld Avatar asked Mar 07 '10 09:03

tarnfeld


People also ask

Do I need to release NSString?

If you create an object using a method that begins with init, new, copy, or mutableCopy, then you own that object and are responsible for releasing it (or autoreleasing it) when you're done with it. If you create an object using any other method, that object is autoreleased, and you don't need to release it.

What does NSString mean?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

What is made up of NSError object?

The core attributes of an NSError object are an error domain (represented by a string), a domain-specific error code and a user info dictionary containing application specific information.


2 Answers

-[NSError localizedDescription].

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

like image 107
kennytm Avatar answered Oct 11 '22 00:10

kennytm


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

[self showAlertWithTitle:@"Error:" withMessage:error.localizedDescription]; 
like image 30
tmr Avatar answered Oct 11 '22 01:10

tmr