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

发布于 2024-07-06 17:36:21 字数 163 浏览 7 评论 0原文

有没有办法抑制 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 技术交流群。

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

发布评论

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

评论(9

久光 2024-07-13 17:36:22

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

#pragma GCC diagnostic ignored "-Wwarning-flag"

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

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

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

#pragma GCC diagnostic ignored "-Wwarning-flag"

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.

时光磨忆 2024-07-13 17:36:22

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

#pragma unused(varname)

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

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

  1. 打开项目 > 编辑活动目标> 构建选项卡。
  2. User-Defined下:找到(如果没有找到则创建)键:GCC_WARN_UNUSED_VARIABLE将其设置为NO

编辑2
示例:

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

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

解决方案:

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

PS:
您还可以设置/重置其他警告:
GCC_WARN_ABOUT_RETURN_TYPE是/否

there is a simpler way to suppress Unused variable warnings:

#pragma unused(varname)

EDIT:
source: 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_VARIABLE set it to NO.

EDIT-2
Example:

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

the compiler shows unused variable warning for 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

淑女气质 2024-07-13 17:36:22

对于 gcc,您可以使用

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

您可以了解 GCC pragma 此处 并获取警告的警告代码转到报告导航器 (Command+9),选择最上面的版本,展开日志(右侧的“=”按钮),然后滚动到底部,您的警告代码位于方括号内像这样 [-Wshadow-ivar]

对于 clang 你可以使用

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

For gcc you can use

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

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

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow-ivar"
// your code
#pragma clang diagnostic pop
过潦 2024-07-13 17:36:22

为了抑制单个文件的警告,请执行以下操作:

在 xcode 项目中选择该文件。
按获取信息
转到包含构建选项的页面
输入 -Wno- 来取消警告:

-Wno-

例如

-Wno-unused-parameter

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

例如

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

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:

-Wno-

e.g.

-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:

e.g.

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

ヤ经典坏疍 2024-07-13 17:36:22

对于 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.

め可乐爱微笑 2024-07-13 17:36:22

要消除警告:尝试为有问题的对象创建一个类别接口

@interface NSTheClass (MyUndocumentedMethodsForNSTheClass)

-(id)theUndocumentedMethod;
@end
...

@implementation myClass : mySuperclass

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

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

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 strongly advise against calling undocumented methods in shipping code. The interface can and will change, and it will be your fault.

不奢求什么 2024-07-13 17:36:22

创建一个名为“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.

毁我热情 2024-07-13 17:36:22

抑制该特定警告并不安全。 编译器需要知道参数的类型并返回方法以生成正确的代码。

例如,如果您调用这样的方法

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

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