17

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)
}
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
AAV
  • 3,785
  • 8
  • 32
  • 59
  • 9
    `-Wno-unused-variable` –  Jul 12 '13 at 19:04
  • 4
    Delete or comment out the unused parts. – Lee Meador Jul 12 '13 at 19:05
  • 6
    The first one, casting to `void` is the most portable and more idiomatic way. – ouah Jul 12 '13 at 19:08
  • 8
    There's also `#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-variable" <#unused variables#> #pragma clang diagnostic pop` – jscs Jul 12 '13 at 19:08
  • Check http://stackoverflow.com/questions/2286312/method-overloading-in-objective-c – ott-- Jul 12 '13 at 22:10
  • @ouah only problem with that is if you have more than one unused parameter then you have to put one more void on next line and it will go on... – AAV Jul 12 '13 at 22:18
  • @AmitVyawahare yes, but if you have more than like 4 parameters maybe you should redesign your API – ouah Jul 12 '13 at 22:30
  • 6
    -Wno-unused-variable is deeply unprofessional. It's like smashing the little speaker that warns you when you are driving without a seatbelt, instead of putting on the seatbelt. Cast to void is idiomatic, and it is valid code on every compiler. – gnasher729 Mar 18 '14 at 21:55
  • 1
    -Wno-unused-variable does not wrk from the command line, nor does -Wno-unused – MarcusJ Jan 08 '15 at 06:10
  • 2
    @gnasher729 not all code is professional, not all code needs to be professional. Apart from that, there are many situations where it's desirable and totally ok to mute almost any particular warning including unused-variable. – johannes_lalala Mar 29 '22 at 22:14

3 Answers3

8

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.

Community
  • 1
  • 1
justin
  • 104,054
  • 14
  • 179
  • 226
  • 1
    I like the idea. I hate left indent #pragma by doing this we can solve that problem as well. – AAV Jul 12 '13 at 22:16
6

If you are compiling with GCC, you can take advantage of attribute extensions to set the 'unused' attribute. Like this:

int somevar __attribute__((unused));

It also works for unused parameter warnings (-Wunused-parameter)

To make it shorter to write I am using this macro:

#define _U_ __attribute__((unused))

And declare like this:

int somevar _U_ ;
Nulik
  • 6,748
  • 10
  • 60
  • 129
-9

One way to do it is just to assign a variable pointlessly after it is declared For example:

int foo;
foo = 0;

This should suppress the unused variable warning. It is just a pointless assignment.
But otherwise I would agree with ouah, the first method is the most reliable, if you must choose from those three.

Owl_Prophet
  • 362
  • 5
  • 15