Turn Off Xcode's Unused Variable Warnings While Typing

Disable one unused variable warning

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

int x __attribute__ ((unused));

or

int y __attribute__((unused)) = initialValue ;

How can I get rid of an unused variable warning in Xcode?

I'm unsure if it's still supported in the new LLVM compiler, but GCC has an "unused" attribute you can use to suppress that warning:

BOOL saved __attribute__((unused)) = [moc save:&error];

Alternatively (in case LLVM doesn't support the above), you could split the variable declaration into a separate line, guaranteeing that the variable would be "used" whether the macro expands or not:

BOOL saved = NO;
saved = [moc save:&error];

Which is the best way to suppress unused variable warning

This is the approach I use: cross platform macro for silencing unused variables warning

It allows you to use one macro for any platform (although the definitions may differ, depending on the compiler), so it's a very portable approach to express your intention to popular compilers for C based languages. On GCC and Clang, it is equivalent of wrapping your third example (#pragma unused(testString)) into a macro.

Using the example from the linked answer:

- (void)testString:(NSString *)testString
{
MONUnusedParameter(testString);
}

I've found this approach best for portability and clarity, in use with some pretty large C, C++, ObjC, and ObjC++ codebases.

How to use typed constants with unused variable warnings?

Make the declaration in your header extern rather that static. What you're doing is creating a variable for every translation unit that includes your header, and this is why Clang is warning you, because it is legitimately a defined variable that is not being used. The extern keyword tells the compiler that the definition of the variable is found somewhere else (it might be in the same translation unit or it might be in another).

In your header, have:

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

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

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

Declaring variables extern allows the compiler to ensure you are treating the variable correctly even if it doesn't know where it is defined (e.g. you can't use it as an NSArray). The linker has the job of making sure you actually defined it somewhere.

How to resolve unused variable compiler warnings?

this is direct way.

  1. use --Wunused-value with gcc this will suppress all warnings for unused value in source code.
  2. As Sourav mentioned in comment try adding -DASNFAM1 while compiling if you want to conditionally compile it.
  3. add #ifdef ASNFAM1 at all occurrences.

I will recommend third option as it is fail proof.
but sometimes we need quick hack so give other 2 a try.
Quick suggestion:- try not to ignore/suppress compiler warnings as they are there for a reason.



Related Topics



Leave a reply



Submit