问题
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?
回答1:
From GCC / Specifying Attributes of Variables (understood by Clang as well):
int x __attribute__ ((unused));
or
int y __attribute__((unused)) = initialValue ;
回答2:
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.
回答3:
__unused int theInt = 0;
// there will be no warning, but you are still able to use `theInt` in the future
回答4:
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
来源:https://stackoverflow.com/questions/21874017/disable-one-unused-variable-warning