有没有办法抑制 Xcode 中的警告?
有没有办法抑制 Xcode 中的警告?
例如,我正在调用一个未记录的方法,并且由于该方法不在标头中,因此我在编译时收到警告。 我知道我可以将其添加到标头中以停止警告,但我想知道除了将其添加到标头(这样我可以保持标头干净和标准)之外是否还有其他方法来抑制警告? 一个pragma什么的?
Is there a way to suppress warnings in Xcode?
For example I am calling an undocumented method and since the method is not in the header I get a warning on compile. I know I can add it to my header to stop the warning, but I am wondering if there is a way other than adding it to the header (so I can keep the headers clean and standard) to suppress the warning? A pragma or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
要在每个文件的基础上禁用警告,使用 Xcode 3 和 llvm-gcc-4.2 可以使用:
其中警告名称是一些 gcc 警告标志。
这会覆盖命令行上的任何警告标志。 但它并不适用于所有警告。 将 -fdiagnostics-show-option 添加到 CFLAGS,您可以看到可以使用哪个标志来禁用该警告。
To disable warnings on a per-file basis, using Xcode 3 and llvm-gcc-4.2 you can use:
Where warning name is some gcc warning flag.
This overrides any warning flags on the command line. It doesn't work with all warnings though. Add -fdiagnostics-show-option to your CFLAGS and you can see which flag you can use to disable that warning.
有一种更简单的方法来抑制未使用的变量警告:
编辑:
来源: http://www.cocoadev.com/index.pl?XCodePragmas
更新:
我遇到了一个新的解决方案,一个更强大的解决方案
User-Defined
下:找到(如果没有找到则创建)键:GCC_WARN_UNUSED_VARIABLE
将其设置为NO
。编辑2
示例:
编译器显示
ok
未使用变量警告。解决方案:
PS:
您还可以设置/重置其他警告:
GCC_WARN_ABOUT_RETURN_TYPE
:是/否
there is a simpler way to suppress Unused variable warnings:
EDIT:
source: http://www.cocoadev.com/index.pl?XCodePragmas
UPDATE:
I came accross with a new solution, a more robust one
User-Defined
: find (or create if you don't find one )the key :GCC_WARN_UNUSED_VARIABLE
set it toNO
.EDIT-2
Example:
the compiler shows unused variable warning for
ok
.Solution:
PS:
You can also set/reset other warning:
GCC_WARN_ABOUT_RETURN_TYPE
:YES/NO
对于 gcc,您可以使用
您可以了解 GCC pragma 此处 并获取警告的警告代码转到报告导航器 (Command+9),选择最上面的版本,展开日志(右侧的“=”按钮),然后滚动到底部,您的警告代码位于方括号内像这样
[-Wshadow-ivar]
对于 clang 你可以使用
For gcc you can use
You can learn about GCC pragma here and to get the warning code of a warning go to the Report Navigator (Command+9), select the topmost build, expand the log (the '=' button on the right), and scroll to the bottom and there your warning code is within square brackets like this
[-Wshadow-ivar]
For clang you can use
为了抑制单个文件的警告,请执行以下操作:
在 xcode 项目中选择该文件。
按获取信息
转到包含构建选项的页面
输入 -Wno- 来取消警告:
例如
如果您查看项目设置,查看位于构建选项卡页面底部的 GCC 警告,您可以获得警告的名称,通过单击每个警告,它会告诉您警告参数名称:
例如
In order to surpress a warning for an individual file do the following:
select the file in the xcode project.
press get info
go to the page with build options
enter -Wno- to negate a warning:
e.g.
You can get the name of the warning if you look on the project settings look at the GCC warnings located at the bottom of the build tab page, by clicking on each warning it will tell you the warning parameter name:
e.g.
对于 Objective-C,许多严重错误仅显示为警告。 我不仅从不禁用警告,而且通常会打开“将警告视为错误”(-Werror)。
通过正确执行操作(通常通过将对象转换为正确的类型)或在需要时声明原型,可以避免代码中的每种类型的警告。
With Objective-C, a number of serious errors only appear as warnings. Not only do I never disable warnings, I normally turn on "Treat warnings as errors" (-Werror).
Every type of warning in your code can be avoided by doing things correctly (normally by casting objects to the correct type) or by declaring prototypes when you need them.
要消除警告:尝试为有问题的对象创建一个类别接口
顺便说一句,我强烈建议不要在运输代码中调用未记录的方法。 界面可以而且将会改变,这将是你的错。
To get rid of the warning: try creating a category interface for the object in question
As an aside, I strongly advise against calling undocumented methods in shipping code. The interface can and will change, and it will be your fault.
http://nshipster.com/pragma/#inhibiting-warnings - 跳到禁止警告部分
http://nshipster.com/pragma/#inhibiting-warnings - skip to inhibiting warnings section
创建一个名为“Undocumented.h”的新的单独头文件并将其添加到您的项目中。 然后为每个要调用未记录函数的类创建一个接口块,并为每个类指定“(未记录)”类别。 然后只需将该头文件包含在您的 PCH 中即可。 这样,您的原始头文件就保持干净,只需要维护一个其他文件,并且您可以注释掉 PCH 中的一行以再次重新启用所有警告。
我还将此方法用于“Depreciated.h”中类别为“(Depreciated)”的折旧函数。
最好的部分是您可以通过注释或取消注释各个原型来选择性地启用/禁用各个警告。
Create a new, separate header file called 'Undocumented.h' and add it to your project. Then create one interface block for each class you want to call undocumented functions on and give each a category of '(Undocumented)'. Then just include that one header file in your PCH. This way your original header files remain clean, there's only one other file to maintain, and you can comment out one line in your PCH to re-enable all the warnings again.
I also use this method for depreciated functions in 'Depreciated.h' with a category of '(Depreciated)'.
the best part is you can selectively enable/disable individual warnings by commenting or uncommenting the individual prototypes.
抑制该特定警告并不安全。 编译器需要知道参数的类型并返回方法以生成正确的代码。
例如,如果您调用这样的方法
[foo doSomethingWithFloat:1.0];
需要一个浮点型,并且没有可见的原型,那么编译器会猜测该方法需要一个双精度型,而不是浮点型。 这可能会导致崩溃和错误解释值。 在上面的示例中,在像 intel 机器这样的小端机器上,接收器方法将看到传递的是 0,而不是 1。
您可以在 i386 ABI 文档,或者您可以修复警告。 :-)
Suppressing that particular warning is not safe. The compiler needs to know the types of the arguments and returns to a method to generate correct code.
For example, if you're calling a method like this
[foo doSomethingWithFloat:1.0];
that takes a float, and there is no prototype visible, then the compiler will guess that the method takes a double, not a float. This can cause crashes and incorrectly interpreted values. In the example above, on a little endian machine like the intel machines, the receiver method would see 0 passed, not 1.
You can read why in the i386 ABI docs, or you can just fix your warnings. :-)