How to use typed constants with warnings "unused variable"?

I am using Xcode 4.6 and I have a header file that contains some constants that I use in all my code. I do not want to use preprocessor directives because I want them to be printed correctly and such.

For example, I have this code in one of my .h files:

static NSString *kErrorCannotDivideByZero = @"Error: Cannot divide by zero"; 

and I use it in the corresponding .m file:

 [self showToast:kErrorCannotDivideByZero]; 

I get a warning:

 /path/to/my/headerFile.h:32:18: Unused variable 'kErrorCannotDivideByZero' 

I only know this warning, but I have about 50 of these warnings cluttering my compiler output.

Why am I getting this warning and how can I solve it?

I don’t just need to suppress all unused variable warnings because I want to get legitimate ones.

+8
c objective-c xcode
source share
6 answers

Make the declaration in the extern header faster than static . What you do creates a variable for each translation unit that includes your title, and that is why Clang warns you because it is a legally defined variable that is not used. The extern keyword tells the compiler that the variable definition is somewhere else (it can be in the same translation unit or it can be in another).

In his headline:

 // declare that the constant exists somewhere extern NSString * const kErrorCannotDivideByZero; 

And in one of your .m files (usually one that has the same name as the header), put

 // define the constant, ie this is where it exists NSString * const kErrorCannotDivideByZero = @"Error: Cannot divide by zero"; 

Declaring extern variables allows the compiler to ensure that the variable is processed correctly even if it does not know where it is defined (for example, you cannot use it as an NSArray ). The linker has the task of making sure that you define it somewhere.

+15
source share

Clang will allow you to click and enter warning flags on the stack and from the "diagnostic" stack: "Managing diagnostics through pragmas" . You can wrap some code snippets as follows:

 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-variable" static NSString *kErrorCannotDivideByZero = @"Error: Cannot divide by zero"; #pragma clang diagnostic pop 

inform the Clan that you know that they are not used, and this is normal in this particular case.

By the way, you may not want to define these variables in a file that is imported to many different places - this is a good way to cause linker errors regarding overriding variables (although this will only happen if the variable has been linked globally) declared / defined without static ) A common template for such constants is to place the extern declaration in the header and define the variable in another file. For more details see the link to the static NSString * const from another class .

As dreamlax pointed out, you really get these warnings because every file that imports your header gets its own copy of the static variable; when I suggested the #pragma technique above, I didn’t understand what you are asking for.

+12
source share

Make your const :

 static NSString * const kErrorCannotDivideByZero = @"Error: Cannot divide by zero"; 

(and as others pointed out, use extern and define in the implementation file)

+4
source share

Perhaps, instead of initializing them for string literals, you can run an initialization function that loads these values ​​from a locale-specific file so that the errors are translated into the target language. When your initialization function assigns this variable, your compiler may be tempted to believe that the variable must exist for successful compilation.

+2
source share

GCC (and I suppose clang) do not warn about unused constants. One of the pitfalls to look out for is that pointers must be pointers to constants, not just pointers to const; therefore, to correctly declare an unused string constant that will not cause any warnings, you need to:

 const char * const myConst = "myConst"; 
+1
source share

You can transfer all declarations of a static variable to your .m file. This should take away all of these "unused variables" warnings. The reason is that static variables are limited by the file level scope.

0
source share

All Articles