What exactly does "__unused" mean?


#1

Hi

I’m struggling a little with understanding the meaning and use of the __unused modifier, or, perhaps the meaning of an unused variable.

If the modifier is not used in the example in the callbacks chapter, for the fetchConn and timer objects, we get a compiler warning saying they are unused variables…

What does it it mean that they are ‘unused’ ? … in the sense that the timer does fire every two seconds and the NSURLConnection *fetchConn connection is made and the process of fetching data begins immediately … so doesn’t mean that they are being ‘used’?

So if they are being used why does the compiler think they are not ?
And if they are not being used …what is the definition of a variable being used?

Thanks for any help

Lenny


#2

It’s just a hint to the compiler that we aren’t going to use that variable directly in a method somewhere else in the file. __unsued just lets Xcode know we know what we are doing and to be quiet about it. :slight_smile:


#3

Your NSTimer object is being used, but the variable named ‘timer’ (which points to that object) isn’t being used after its initial assignment. This is what the compiler is warning. Placing ‘__unused’ before the variable declaration tells the compiler you know what is happening, and you don’t care that the variable is going unused.

Another way to silence the compiler warning would be to break the variable assignment and initialization into two statements:

NSTimer * timer;
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:logger selector:@selector(sayOuch:) userInfo:nil repeats:YES];

In this case, the variable named ‘timer’ is declared on the first line, and then that variable is “used” when it is given the address of your NSTimer object.


#4

Thanks macshome and jaycampbell

Appreciate your help, thanks for clarifying ,I understand the concept now.

Cheers

Lenny