101

I understand exactly why unused variable warnings occur. I don't want to suppress them in general, because they are incredibly useful in most cases. However, consider the following (contrived) code.

NSError *error = nil;
BOOL saved = [moc save:&error];
NSAssert1(saved, @"Dude!!1! %@!!!", error);

Xcode reports that saved is an unused variable, when of course it isn't. I suspect this is because NSAssert1 is a macro. The NS_BLOCK_ASSERTIONS macro is not defined, so Objective C assertions are definitely enabled.

While it doesn't hurt anything, I find it untidy and annoying, and I want to suppress it, but I'm not sure how to do so. Assigning the variable to itself gets rid of the compiler warning, but I'd rather do it the "right" way if such a thing exists.

jscs
  • 63,694
  • 13
  • 151
  • 195
Gregory Higley
  • 15,923
  • 9
  • 67
  • 96

10 Answers10

112

Using Xcode 4.3.2 and found out that this seems to work (less writing)

BOOL saved __unused;
JOM
  • 8,139
  • 6
  • 78
  • 111
110

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];
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
Sherm Pendley
  • 13,556
  • 3
  • 45
  • 57
38

In Xcode you can set the warnings for "Unused Variables." Go to "Build Settings" for the target and filter with the word "unused"

Here is a screenshot: Builld Settings Screenshot

I suggest you only change it for Debug. That way you don't miss anything in your release version.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • 37
    Did you miss the part of the question where Gregory said "I don't want to suppress them in general, because they are incredibly useful in most cases"? – Sherm Pendley Mar 27 '11 at 20:21
  • 9
    @ShermPendley Regardless, this is still useful, it answered my question on how to turn it off globally. – raffian Sep 23 '12 at 22:35
  • 9
    turning off warnings is a great way to miss bugs. I highly recommend against. – orion elenzil May 02 '13 at 06:06
  • 2
    @orionelenzil - well, yes and no. There is a real interface problem with false positives. The warning pops up constantly while you're writing code if you haven't gotten to the part of the code where you use the variable. Over time, this will train you to ignore the warning meaning you may well miss more important errors. It would better to be able to suppress the error in blocks of code being actively edited. – TechZen Mar 11 '14 at 18:43
  • Hi, its working fine if i set NO. but my doubt is : i have to set Debug & Release NO ya only Unused Variable: NO? which one i have to set. If i set Totally NO, then its not create any prob during release na? – Soumya Ranjan Aug 18 '14 at 10:23
22
NSError *error = nil;
BOOL saved = [moc save:&error];
NSAssert1(saved, @"Dude!!1! %@!!!", error);
#pragma unused(saved)

Try like this. It is working for me. It will work for you, too.

Danny Xu
  • 401
  • 3
  • 10
16

The only simple and portable way to mark variable as used is… to use it.

BOOL saved = ...;
(void)saved; // now used

You may be happy with already described compiler-specific extensions, though.

user3125367
  • 2,920
  • 1
  • 17
  • 17
15
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
    NSUInteger abc; /// Your unused variable
#pragma clang diagnostic pop

SOURCE

Alex
  • 8,908
  • 28
  • 103
  • 157
  • This is the cleanest approach since it disables only the warning between those #pragmas – cornr Dec 19 '17 at 13:11
9

try with: __unused attribute. Works in Xcode 5

João Nunes
  • 3,751
  • 31
  • 32
6

This is the way you do it in C and therefore also Objective-C.

Even though you do not have warnings enabled, it's always a good idea to mark the return value as explicitly ignored. It also goes to show other developers, that you have not just forgotten about the return value – you have indeed explicitly chosen to ignore it.

(void)[moc save:&error];

EDIT: Compilers ignore casts to void, so it should not affect performance – it's just a nice clean human annotation.

Trenskow
  • 3,783
  • 1
  • 29
  • 35
  • In certain contexts you MUST use this hack to get the Objective-C code to compile. Using `(void)` is the clearest way to go, thanks! – Dan Rosenstark Sep 21 '17 at 09:26
5

You can set "No" LLVM compliler 2.0 warning on "Release" enter image description here

ArNo
  • 2,278
  • 1
  • 22
  • 19
1

Make it take up two lines. Separate the declaration and default value

BOOL enabled = NO;

// ...

BOOL enabled;

enabled = NO;
0xFADE
  • 832
  • 5
  • 7