iosobjective-ccclang

Which is the best way to suppress "unused variable" warning


There are 3 (which I know) ways to suppress the "unused variable" warning. Any particular way is better than other ?

First

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

Second

- (void)testString:(NSString *)__unused testString
{

}

Third

- (void)testString:(NSString *)testString
{
    #pragma unused(testString)
}

Solution

  • 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.