Xcode 4 warning "Expression result unused” for NSURLConnection

Johann picture Johann · Oct 27, 2011 · Viewed 18.1k times · Source

I'm just trying to do my usual data transfert. I define my NSMutableURLRequest then call

[[NSURLConnection alloc] initWithRequest:request delegate:self];

This used to be ok with Xcode 3 but Xcode 4 warns me about "Expression result unused" on that line. The request does work but I would like to find a way to get rid of the warning.

I suppose I could store the connection in a variable but I don't really need it and I can't see the point of setting it to nil the next line (although this would remove the warning)

Please note: I'm not 100% sure if it's Xcode 4 or the fact ARC is enabled.

Answer

progrmr picture progrmr · Oct 27, 2011

When a function returns a result that you don't need you can cast it to void to eliminate the compiler warning:

(void) [[NSURLConnection alloc] initWithRequest:request delegate:self];

I haven't used ARC yet so I can't say if this is a good idea, before ARC you would need to keep this pointer result somewhere so you could release it.