抑制 Xcode 中已弃用的警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2622017/
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
Suppressing deprecated warnings in Xcode
提问by Ben Gottlieb
With all the SDKs floating around, it's handy to be able to build for multiple SDKs and platforms. However, bouncing from 3.2 to 3.0 and even occasionally 2.x, I frequently get deprecated warnings involving methods that have changed or been superseded:
由于所有 SDK 都在浮动,因此能够为多个 SDK 和平台进行构建非常方便。但是,从 3.2 跳到 3.0 甚至偶尔跳到 2.x,我经常收到涉及已更改或被取代的方法的弃用警告:
warning: 'UIKeyboardBoundsUserInfoKey' is deprecated.
Since I still want to maintain compatibility with older OSes, and I'm also striving to remove 'noise' when building, is there a way to turn off or disable these warnings?
由于我仍然想保持与旧操作系统的兼容性,并且我也在努力消除构建时的“噪音”,有没有办法关闭或禁用这些警告?
采纳答案by Paul R
Try -Wno-deprecated-declarations
, or its corresponding setting in Xcode, GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS
(pro tip: just type in "deprecated" in the build settings to find the specific setting for this warning).
Try -Wno-deprecated-declarations
,或其在 Xcode 中的相应设置,GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS
(专业提示:只需在构建设置中输入“已弃用”即可找到此警告的特定设置)。
Current versions of Xcode (e.g. Xcode 9.2):
当前版本的 Xcode(例如 Xcode 9.2):
Ancient versions of Xcode (e.g. Xcode 2.x, 3.x):
旧版本的 Xcode(例如 Xcode 2.x、3.x):
回答by manicaesar
Since I yet can not add a comment to the @samiq post, I think I will expand it. Input mentioned directive before a function / method in which you use deprecated stuff. Then you can restore the previous setting after the definition of the function end:
由于我还不能在@samiq 帖子中添加评论,我想我会扩展它。在使用不推荐使用的东西的函数/方法之前输入提到的指令。然后就可以在函数定义结束后恢复之前的设置:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
- (void) methodUsingDeprecatedStuff {
//use deprecated stuff
}
#pragma GCC diagnostic pop
回答by Andrew Hershberger
Clang provides a nice feature that makes the "restore" step in the @manicaesar post independent of the initial warning state:
Clang 提供了一个很好的功能,使@manicaesar 帖子中的“恢复”步骤独立于初始警告状态:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void) methodUsingDeprecatedStuff {
//use deprecated stuff
}
#pragma clang diagnostic pop
To quote the Clang manual:
引用 Clang手册:
In addition to all of the functionality provided by GCC's pragma, Clang also allows you to push and pop the current warning state. This is particularly useful when writing a header file that will be compiled by other people, because you don't know what warning flags they build with.
除了 GCC 编译指示提供的所有功能之外,Clang 还允许您推送和弹出当前警告状态。这在编写将由其他人编译的头文件时特别有用,因为您不知道他们构建的警告标志是什么。
回答by Joe Hughes
Since we tend to need to support older OSes, but pay attention to our warnings, I wanted a tidier way to do this. I put this together, inspired by some Mozilla code:
由于我们往往需要支持较旧的操作系统,但要注意我们的警告,我想要一种更整洁的方法来做到这一点。我把它放在一起,灵感来自一些 Mozilla 代码:
#define SILENCE_DEPRECATION(expr) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \
expr; \
_Pragma("clang diagnostic pop") \
} while(0)
#define SILENCE_IOS7_DEPRECATION(expr) SILENCE_DEPRECATION(expr)
#define SILENCE_IOS8_DEPRECATION(expr) SILENCE_DEPRECATION(expr)
This allows you to do the following:
这允许您执行以下操作:
SILENCE_IOS7_DEPRECATION(return [self sizeWithFont:font constrainedToSize:size]);
It also works with blocks of code:
它也适用于代码块:
SILENCE_IOS7_DEPRECATION(
view = [[MKPolylineView alloc] initWithPolyline:self];
view.lineWidth = self.lineWidth;
view.strokeColor = self.color;
);
Also, when you do drop support for pre-iOS 7 devices, you can easily search through the code to find the deprecated usages to fix.
此外,当您不支持 iOS 7 之前的设备时,您可以轻松搜索代码以找到要修复的弃用用法。
回答by samiq
You can also suppress warnings per file by using
您还可以通过使用抑制每个文件的警告
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
which in turn makes it a little bit better practice than just suppressing all warning once and together... after all you got to know what you are doing it for.
这反过来又使它成为比一次性抑制所有警告更好的实践......毕竟你知道你在做什么。
回答by krzysztof
If you want to silence warning Implementing deprecated methodor Implementing deprecated class, use:
如果您想消除警告实现已弃用的方法或实现已弃用的类,请使用:
#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-implementations" // code #pragma clang diagnostic pop
回答by jarora
If you would like a blanket check for all kinds of deprecations in a piece of code. Please use the -Wdeprecatedflag like below:
如果您想对一段代码中的各种弃用进行全面检查。请使用-Wdeprecated标志,如下所示:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
- (void) methodUsingDeprecatedStuff {
//use deprecated stuff
}
#pragma clang diagnostic pop
回答by harvestli
To disable warning from third-party header file, add following line at the top of file
要禁用来自第三方头文件的警告,请在文件顶部添加以下行
#pragma clang system_header