Xcode 4 warning "Expression result unused” for NSURLConnection

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.


progrmr's answer is correct, but here's an even cleaner way to do it:

[NSURLConnection connectionWithRequest:request delegate:self];

This doesn't cause a warning, even if you don't cast the result to void.


Someone should be responsible for that NSURLConnection. It is not needed to store the connection but it is better coding if you do. The problem is that after you created our NSURLConnection no one has a pointer to that created instance which should not be the case.

Let's assume the following example:

  1. your instance of ClassA is creating an instane of NSURLConnection
  2. your instance of ClassA is beeing released and dealloced
  3. NSURLConnection is still alive and will fire the delegate to your deallocated instance.

To solve that problem you should store the instance of NSURLConnection and should release that connection if your instance of ClassA is being dealloced which results in deallocating the instance of NSURLConnection as well.