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

半世苍凉 提交于 2019-11-27 17:55:31
Sherm Pendley

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

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

BOOL saved __unused;

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:

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

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.

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.

try with: __unused attribute. Works in Xcode 5

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

SOURCE

You can set "No" LLVM compliler 2.0 warning on "Release"

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.

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

BOOL enabled = NO;

// ...

BOOL enabled;

enabled = NO;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!