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

Objective CXcodeCompiler WarningsUnused Variables

Objective C Problem Overview


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.

Objective C Solutions


Solution 1 - Objective C

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];

Solution 2 - Objective C

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

BOOL saved __unused;

Solution 3 - Objective C

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.

Solution 4 - Objective C

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.

Solution 5 - Objective C

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.

Solution 6 - Objective C

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
    NSUInteger abc; /// Your unused variable
#pragma clang diagnostic pop

SOURCE

Solution 7 - Objective C

try with: __unused attribute. Works in Xcode 5

Solution 8 - Objective C

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.

Solution 9 - Objective C

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

Solution 10 - Objective C

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

BOOL enabled = NO;

// ...

BOOL enabled;

enabled = NO;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGregory HigleyView Question on Stackoverflow
Solution 1 - Objective CSherm PendleyView Answer on Stackoverflow
Solution 2 - Objective CJOMView Answer on Stackoverflow
Solution 3 - Objective CBlack FrogView Answer on Stackoverflow
Solution 4 - Objective CDanny XuView Answer on Stackoverflow
Solution 5 - Objective Cuser3125367View Answer on Stackoverflow
Solution 6 - Objective CAlexView Answer on Stackoverflow
Solution 7 - Objective CJoão NunesView Answer on Stackoverflow
Solution 8 - Objective CTrenskowView Answer on Stackoverflow
Solution 9 - Objective CArNoView Answer on Stackoverflow
Solution 10 - Objective C0xFADEView Answer on Stackoverflow