我怎样才能摆脱“未使用的变量”? Xcode 中出现警告?

发布于 2024-10-27 11:40:06 字数 457 浏览 5 评论 0原文

我完全理解为什么会出现未使用的变量警告。一般来说,我不想压制它们,因为它们在大多数情况下非常有用。但是,请考虑以下(人为的)代码。

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

Xcode 报告 saved 是一个未使用的变量,当然事实并非如此。我怀疑这是因为 NSAssert1 是一个宏。 NS_BLOCK_ASSERTIONS定义,因此 Objective C 断言肯定是启用的。

虽然它不会伤害任何东西,但我觉得它又乱又烦人,我想抑制它,但我不知道该怎么做。将变量分配给自身可以消除编译器警告,但如果存在这种情况,我宁愿以“正确”的方式进行操作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

郁金香雨 2024-11-03 11:40:06

使用Xcode 4.3.2并发现这似乎有效(少写)

BOOL saved __unused;

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

BOOL saved __unused;
滴情不沾 2024-11-03 11:40:06

我不确定新的 LLVM 编译器是否仍然支持它,但 GCC 有一个“未使用”属性,您可以使用它来抑制该警告:

BOOL saved __attribute__((unused)) = [moc save:&error];

或者(如果 LLVM 不支持上述内容),您可以将变量声明拆分为一个单独的行,保证无论宏是否展开,变量都会被“使用”:

BOOL saved = NO;
saved = [moc save:&error];

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];
小傻瓜 2024-11-03 11:40:06

在 Xcode 中,您可以设置“未使用的变量”的警告。转到目标的“构建设置”,并使用“未使用”一词进行过滤。

这是一个屏幕截图: Builld Settings Screenshot

I建议您只更改它以进行调试。这样您就不会错过发布版本中的任何内容。

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.

羞稚 2024-11-03 11:40:06
NSError *error = nil;
BOOL saved = [moc save:&error];
NSAssert1(saved, @"Dude!!1! %@!!!", error);
#pragma unused(saved)

尝试这样。
它对我有用。它也会对你有用。

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.

没︽人懂的悲伤 2024-11-03 11:40:06

将变量标记为已使用的唯一简单且可移植的方法是……使用它。

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

不过,您可能会对已经描述的特定于编译器的扩展感到满意。

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.

时间海 2024-11-03 11:40:06
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
    NSUInteger abc; /// Your unused variable
#pragma clang diagnostic pop

来源

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

SOURCE

浮生面具三千个 2024-11-03 11:40:06

尝试使用:__unused 属性。适用于 Xcode 5

try with: __unused attribute. Works in Xcode 5

帅冕 2024-11-03 11:40:06

您可以在“发布”上设置“否”LLVM 编译器 2.0 警告 在此处输入图像描述

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

他夏了夏天 2024-11-03 11:40:06

这是 C 语言的方式,因此 Objective-C 也是如此。

即使您没有启用警告,将返回值标记为显式忽略始终是一个好主意。它还向其他开发人员表明,您不仅仅是忘记了返回值 - 您确实明确选择了忽略它。

(void)[moc save:&error];

编辑:编译器忽略对 void 的强制转换,因此它不应该影响性能 - 这只是一个很好的干净的人类注释。

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.

〆凄凉。 2024-11-03 11:40:06

让它占据两行。将声明和默认值分开

BOOL enabled = NO;

// ...

BOOL enabled;

enabled = NO;

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

BOOL enabled = NO;

// ...

BOOL enabled;

enabled = NO;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文