Xcode/iOS——摆脱特定常量的弃用警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6481808/
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
Xcode/iOS -- get rid of deprecation warnings for specific constants?
提问by William Jockusch
I have some deprecated constants in my project. They need to stay. I don't want to be warned about them, but do want to be warned if other deprecated constants should turn up in my project later.
我的项目中有一些已弃用的常量。他们需要留下来。我不想收到关于它们的警告,但如果其他不推荐使用的常量稍后出现在我的项目中,我希望收到警告。
Apple's header declares them as follows:
Apple 的标题将它们声明如下:
extern NSString * const NameOfStringConstant __OSX_AVAILABLE_BUT_DEPRECATED(version availability info here)
How can I silence the warning?
我怎样才能使警告静音?
Related answer for silencing the warning for a deprecated method here
Related answer for silencing the warning about a deprecated string conversion here
回答by Rok Jarc
I know this is an old topic but today i was dealing with the same annoyance.
我知道这是一个老话题,但今天我正在处理同样的烦恼。
Example: you want to get rid of the annoying deprecation warning but just for [[UIDevice currentDevice] uniqueIdentifier]]
since you most probably want to use it in development phase with TestFlight.
You'd still want the compiler to warn you if you use some other deprecated declaration by mistake.
示例:您想摆脱烦人的弃用警告,但只是[[UIDevice currentDevice] uniqueIdentifier]]
因为您很可能希望在开发阶段使用 TestFlight。如果您错误地使用了其他不推荐使用的声明,您仍然希望编译器警告您。
I like sarfata's answer: it does the job. But there's more politically correctway available:
我喜欢sarfata 的回答:它可以完成工作。但是还有更多正确的方法可用:
Following recipe is taken from The Goo Software Blog.
以下配方取自The Goo Software Blog。
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[TestFlight setDeviceIdentifier:[[UIDevice currentDevice] uniqueIdentifier]];
#pragma clang diagnostic pop
Make sure you comment out this lines before building for distribution. Or simply use a preprocessor macro to exclude this lines from a release build.
确保在构建分发之前注释掉这些行。或者简单地使用预处理器宏从发布版本中排除此行。
回答by senojsitruc
Add to the compiler flags:
添加到编译器标志:
-Wno-deprecated-declarations
or, in Xcode, select "No" for the build setting option:
或者,在 Xcode 中,为构建设置选项选择“否”:
Warn About Deprecated Functions
and then if you look at the build output (Apple+7 in Xcode 4), you'll notice the aforementioned compiler flag.
然后,如果您查看构建输出(Xcode 4 中的 Apple+7),您会注意到上述编译器标志。
回答by sarfata
This is #1 answer in google and I believe there are some faire case when using a deprecated method is useful and when you want to avoid warnings to keep the build "clean". This solutions is inspired by: http://vgable.com/blog/2009/06/15/ignoring-just-one-deprecated-warning/
这是 google 中的 #1 答案,我相信当使用已弃用的方法很有用并且您想避免警告以保持构建“干净”时,会有一些公平的情况。此解决方案的灵感来自:http: //vgable.com/blog/2009/06/15/ignoring-just-one-deprecated-warning/
The idea is to declare a new protocol that has the same method (but not deprecated of course) and to cast the object to that protocol. This way you can call the method without getting the warning and without getting rid of all the deprecation warnings.
这个想法是声明一个具有相同方法的新协议(但当然不推荐使用)并将对象转换为该协议。通过这种方式,您可以调用该方法而不会收到警告,也不会消除所有弃用警告。
Exemple: If you want to integrate TestFlight in your application, the SDK documentation suggests transmitting the uniqueIdentifier of the device while in BETA. This can help track which tester had problems. This method is deprecated by Apple (and they will not let you submit the app) but I believe this is a good example of using a deprecated method.
例如:如果您想在您的应用程序中集成 TestFlight,SDK 文档建议在 BETA 中传输设备的 uniqueIdentifier。这可以帮助跟踪哪个测试人员有问题。Apple 已弃用此方法(他们不会让您提交应用程序),但我相信这是使用已弃用方法的一个很好的示例。
In your App Delegate:
在您的应用委托中:
/* This is to avoid a warning when calling uniqueIdentifier for TestFlight */
@protocol UIDeviceHack <NSObject>
- (NSString*) uniqueIdentifier;
@end
@implementation MyAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[TestFlight takeOff:@"Your-Testflight-team-id"];
// TODO: Remove this in production (forbidden APIs) - Used here to improve beta reporting.
[TestFlight setDeviceIdentifier:[(id<UIDeviceHack>)[UIDevice currentDevice] uniqueIdentifier]];
// ...
}
回答by jer
The proper answer to this question is to not use deprecated constants. Check the documentation for the recommended way to accomplish something now. On the deprecated methods/constants/whatever, there's almost always a link to the "replacement" if you will. Use that instead. This way your code doesn't mysteriously break when those disappear forever, but your users still have a build built against the old sdk, and now their code crashes, or worse, does weird things.
这个问题的正确答案是不使用不推荐使用的常量。检查文档以了解现在完成某事的推荐方法。在不推荐使用的方法/常量/任何东西上,如果您愿意,几乎总是有一个指向“替换”的链接。改用那个。这样你的代码不会在它们永远消失时神秘地中断,但是你的用户仍然有一个针对旧 sdk 的构建,现在他们的代码崩溃了,或者更糟的是,做了一些奇怪的事情。