objective-c 有没有办法抑制 Xcode 中的警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/194666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:50:04  来源:igfitidea点击:

Is there a way to suppress warnings in Xcode?

objective-cxcodecocoacocoa-touchsuppress-warnings

提问by kdbdallas

Is there a way to suppress warnings in Xcode?

有没有办法抑制 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?

例如,我正在调用一个未记录的方法,并且由于该方法不在标题中,因此我在编译时收到警告。我知道我可以将它添加到我的标题中以停止警告,但我想知道除了将它添加到标题(这样我可以保持标题干净和标准)之外是否还有其他方法来抑制警告?一个 pragma 什么的?

回答by robottobor

To disable warnings on a per-file basis, using Xcode 3 and llvm-gcc-4.2 you can use:

要在每个文件的基础上禁用警告,您可以使用 Xcode 3 和 llvm-gcc-4.2:

#pragma GCC diagnostic ignored "-Wwarning-flag"

Where warning name is some gcc warning flag.

其中警告名称是一些 gcc 警告标志。

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.

这会覆盖命令行上的任何警告标志。但是,它不适用于所有警告。将 -fdiagnostics-show-option 添加到您的 CFLAGS,您可以查看可以使用哪个标志来禁用该警告。

回答by thesummersign

there is a simpler way to suppress Unused variablewarnings:

有一种更简单的方法可以抑制未使用的变量警告:

#pragma unused(varname)

EDIT: source: http://www.cocoadev.com/index.pl?XCodePragmas

编辑:来源:http: //www.cocoadev.com/index.pl?XCodePragmas

UPDATE: I came accross with a new solution, a more robust one

更新:我遇到了一个新的解决方案,一个更强大的解决方案

  1. Open the Project > Edit Active Target> Build tab.
  2. Under User-Defined: find (or create if you don't find one )the key : GCC_WARN_UNUSED_VARIABLEset it to NO.
  1. 打开项目 > 编辑活动目标 > 构建选项卡。
  2. User-Defined:查找(如果找不到,则创建)键:GCC_WARN_UNUSED_VARIABLE将其设置为NO.

EDIT-2 Example:

EDIT-2 示例:

BOOL ok = YES;
NSAssert1(ok, @"Failed to calculate the first day the month based on %@", self);

the compiler shows unused variable warning for ok.

编译器显示未使用的变量警告ok

Solution:

解决方案:

BOOL ok = YES;
#pragma unused(ok)
NSAssert1(ok, @"Failed to calculate the first day the month based on %@", self);

PS: You can also set/reset other warning: GCC_WARN_ABOUT_RETURN_TYPE: YES/NO

PS:您还可以设置/重置其他警告 GCC_WARN_ABOUT_RETURN_TYPE::YES/NO

回答by Inder Kumar Rathore

For gcc you can use

对于 gcc,您可以使用

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow-ivar"
// your code
#pragma GCC diagnostic pop

You can learn about GCC pragma hereand 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]

您可以在此处了解GCC pragma并获取警告的警告代码,请转到 Report Navigator (Command+9),选择最顶层的构建,展开日志(右侧的“=”按钮),然后滚动到底部和那里你的警告代码在这样的方括号内[-Wshadow-ivar]

For clang you can use

对于叮当,您可以使用

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow-ivar"
// your code
#pragma clang diagnostic pop

回答by AndersK

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:

选择 xcode 项目中的文件。按获取信息转到带有构建选项的页面输入 -Wno- 以取消警告:

-Wno-

-Wno-

e.g.

例如

-Wno-unused-parameter

-Wno-unused-parameter

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:

如果您查看项目设置,查看位于构建选项卡页面底部的 GCC 警告,您可以获得警告的名称,通过单击每个警告,它会告诉您警告参数名称:

e.g.

例如

Warn whenever a function parameter is unused aside from its declaration. [GCC_WARN_UNUSED_PARAMETER, -Wunused-parameter]

每当函数参数除了声明之外未使用时发出警告。[GCC_WARN_UNUSED_PARAMETER,-Wunused-参数]

回答by Austin France

回答by Matt Gallagher

With Objective-C, a number of serious errors only appear as warnings. Not only do I neverdisable warnings, I normally turn on "Treat warnings as errors" (-Werror).

在 Objective-C 中,一些严重的错误只会以警告的形式出现。我不仅从不禁用警告,而且通常打开“将警告视为错误”(-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.

通过正确地做事(通常通过将对象转换为正确的类型)或在需要时声明原型,可以避免代码中的每种类型的警告。

回答by Mark Pauley

To get rid of the warning: try creating a category interface for the object in question

摆脱警告:尝试为有问题的对象创建一个类别界面

@interface NSTheClass (MyUndocumentedMethodsForNSTheClass)

-(id)theUndocumentedMethod;
@end
...

@implementation myClass : mySuperclass

-(void) myMethod {
...
   [theObject theUndocumentedMethod];
...
}

As an aside, I stronglyadvise against calling undocumented methods in shipping code. The interface can and will change, and it will be your fault.

顺便说一句,我强烈建议不要在运输代码中调用未记录的方法。界面可以而且将会改变,这将是你的错。

回答by Mark A. Donohoe

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.

创建一个名为“Undocumented.h”的新的独立头文件并将其添加到您的项目中。然后为每个要调用未记录函数的类创建一个接口块,并为每个类指定“(未记录)”类别。然后只需在您的 PCH 中包含该头文件。这样您的原始头文件保持干净,只有一个其他文件需要维护,您可以在 PCH 中注释掉一行以再次重新启用所有警告。

I also use this method for depreciated functions in 'Depreciated.h' with a category of '(Depreciated)'.

我也将此方法用于“Depreciated.h”中的“(Depreciated)”类别的折旧函数。

the best part is you can selectively enable/disable individual warnings by commenting or uncommenting the individual prototypes.

最好的部分是您可以通过注释或取消注释单个原型来选择性地启用/禁用单个警告。

回答by Ken

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

[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.

这需要一个浮点数,并且没有可见的原型,那么编译器会猜测该方法需要一个双精度数,而不是一个浮点数。这可能会导致崩溃和错误解释的值。在上面的例子中,在像 intel 机器这样的小端机器上,接收器方法会看到 0 通过,而不是 1。

You can read why in the i386 ABI docs, or you can just fix your warnings. :-)

您可以在i386 ABI 文档中阅读原因,或者您可以修复您的警告。:-)