Question

I started looking into network programming for iOS. I was following a tutorial and stopped because I was getting a "Variable never used" warning. I found another tutorial that handled it differently but it also creates a warning. Then, I decided to check Apple's sample code and turns out they do it another way too!

Tutorial 1: http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request 
                                                        delegate:self];

Tutorial 2: http://www.cimgf.com/2010/02/12/accessing-the-cloud-from-cocoa-touch/

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

Apple: https://developer.apple.com/library/ios/samplecode/SimpleURLConnections/Listings/PostController_m.html#//apple_ref/doc/uid/DTS40009245-PostController_m-DontLinkElementID_12

// Declare property in .m file so it doesn't show as public
@property (nonatomic, strong, readwrite) NSURLConnection *  connection;
// Then just assign to it when the connection is created
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

Apple's method gets rid of the warning, but it seems a bit wasteful to assign to a variable that will never be read again. Is that the best practice?

Was it helpful?

Solution 2

The variable won't "never" be used.

connectionWithRequest:delegate: doesn't actually start any networking. You must call the start method on the variable before the connection is actually started. And after its been started, you can call the cancel method on the variable to stop the connection.


EDIT: Per Rob's comment, connectionWithRequest: and most of the NSURLConnection instantiation methods actually start the request. The variable is still returned though in case you want to cancel (and there are probably other things you can do with it).

But if you never want to do anything with the variable after you initiate the connection, there are other ways to create a connection. I'll edit them in.

You can use sendAsynchronousRequest:queue:completionHandler:, which returns void.

For this method, you send your request argument, and you give a queue for the connection to work on, and a block that is executed when the connection is done.

You can also do as Rob suggests and just not put the return value anywhere:

[NSURLConnection connectionWithRequest:request delegate:self];

If for whatever reason, you also don't like this, you can do this:

@autoreleasepool {
    NSURLConnection *foo = [NSURLConnection connectionWithRequest:request delegate:self];
#pragma unused(foo)
}

Which I imagine will be optimized during compilation straight down to the previous example of just:

[NSURLConnection connectionWithRequest:request delegate:self];

As a note, #pragma unused(someVar) will suppress unused variable warnings for the given variable.

OTHER TIPS

If you don't need the NSURLConnection variable, just do:

[NSURLConnection connectionWithRequest:request delegate:self];

This prevents the compiler warnings you describe in your question.

If you want the ability to cancel the request at a later point (e.g. the user dismisses the view controller while the connection is underway), then you'd save the NSURLConnection in a property, and then you have the ability to cancel the request when the view controller is dismissed.

Otherwise, the syntax shown above initiates the connection with no variables and no compiler warnings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top