Xcode 4 warning "Expression result unused” for NSURLConnection

18,204

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
18,204
Johann
Author by

Johann

Based in Marseille (France), I'm the Chief Innovation Officer for BigChange a british company. I mostly code in JQuery, c# and have done a few apps for iPhone and Android.

Updated on June 02, 2022

Comments

  • Johann
    Johann about 2 years

    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.

  • Johann
    Johann over 12 years
    I can't release it as ARC is on. Is it not what ARC is supposed to do automatically, deallocating the pointer?
  • Johann
    Johann over 12 years
    This did remove the warning and the function is still working as required so all good! It's the first time I see this cast to void. Is it good practice? Thanks!
  • progrmr
    progrmr over 12 years
    Yes, because it documents the fact that you know there is a result being returned but you don't need that result.
  • thomas
    thomas over 12 years
    but the problem still exist semantically: NSURLConnection knows your instance of ClassA but nobody knows your instance. There is a relationship between both instances and this relationship needs to be managed ;) But if you dont save the pointer to NSURLConnection then nobody has a pointer to it and either ARC is deallocating the connection immediately or never. If you save the resulting pointer then the warning will not appear and you have a better coding-design and you are then able write more reliable code:connection:(NSURLConnection*)con didSth{if(con != self.curCon) return;}
  • Dan F
    Dan F about 10 years
    I still get the warning when I am attempting to use a #define based logging macro: #if SHOW_DEBUG_LOG # define DEBUG_LOG(x,...) NSLog(@"Debug Log: "x, __VA_ARGS__) #else # define DEBUG_LOG(x,...) (void)(x); do { (void)(__VA_ARGS__); } while (0) #endif
  • Seva Alekseyev
    Seva Alekseyev over 4 years
    Deprecated as of iOS 10. :(