user2159978
user2159978

Reputation: 2709

Disable one unused variable warning

How can I disable one warning in one place only?

I have one variable which I temporarily don't use. Xcode shows me a warning about the "unused variable". I want to disable the warning, but for this variable only, not all warnings of this type.

Is it possible without setting/getting this variable's value?

Upvotes: 7

Views: 3138

Answers (4)

OlDor
OlDor

Reputation: 1466

__unused int theInt = 0;
// there will be no warning, but you are still able to use `theInt` in the future

Upvotes: 3

Martin R
Martin R

Reputation: 539685

From GCC / Specifying Attributes of Variables (understood by Clang as well):

int x __attribute__ ((unused));

or

int y __attribute__((unused)) = initialValue ;

Upvotes: 7

CRD
CRD

Reputation: 53000

Shortest keystroke answer, change:

int x; // this variable temporarily unused

to:

// int x; // this variable temporarily unused

and the warning will go. Also you cannot forget to remove it when you need the variable again, which you can do with any approach which leaves the variable declared.

If you want the removal to be a bit more visible, but wish to keep the property that you can't forget to remove it, try:

#if 0
int x; // this variable temporarily unused
#endif

HTH

Upvotes: 0

Tomasz Szulc
Tomasz Szulc

Reputation: 4235

It's pretty simple:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
    NSUInteger abc; /// Your unused variable
#pragma clang diagnostic pop

But keeping unused variable is redundant and generally bad idea. Unused code should be removed. If you use git, there all changes are still in your repo and you can revert your code if you found that this variable is necessary.

Upvotes: 10

Related Questions