我怎样才能摆脱“未使用的变量”? Xcode 中出现警告?
我完全理解为什么会出现未使用的变量警告。一般来说,我不想压制它们,因为它们在大多数情况下非常有用。但是,请考虑以下(人为的)代码。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用Xcode 4.3.2并发现这似乎有效(少写)
Using Xcode 4.3.2 and found out that this seems to work (less writing)
我不确定新的 LLVM 编译器是否仍然支持它,但 GCC 有一个“未使用”属性,您可以使用它来抑制该警告:
或者(如果 LLVM 不支持上述内容),您可以将变量声明拆分为一个单独的行,保证无论宏是否展开,变量都会被“使用”:
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:
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:
在 Xcode 中,您可以设置“未使用的变量”的警告。转到目标的“构建设置”,并使用“未使用”一词进行过滤。
这是一个屏幕截图:
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:
I suggest you only change it for Debug. That way you don't miss anything in your release version.
尝试这样。
它对我有用。它也会对你有用。
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.
You may be happy with already described compiler-specific extensions, though.
来源
SOURCE
尝试使用:__unused 属性。适用于 Xcode 5
try with: __unused attribute. Works in Xcode 5
您可以在“发布”上设置“否”LLVM 编译器 2.0 警告
You can set "No" LLVM compliler 2.0 warning on "Release"
这是 C 语言的方式,因此 Objective-C 也是如此。
即使您没有启用警告,将返回值标记为显式忽略始终是一个好主意。它还向其他开发人员表明,您不仅仅是忘记了返回值 - 您确实明确选择了忽略它。
编辑:编译器忽略对
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.
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