在 Xcode 的特定行中禁用特定警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3137414/
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
Disabling a specific warning in a specific line in Xcode
提问by David Coufal
I'm writing an iPhone app against the Base 4.0 SDK, but I'm targeting OS 3.1.3 so OS 3 users can use the app.
我正在针对 Base 4.0 SDK 编写 iPhone 应用程序,但我的目标是 OS 3.1.3,因此 OS 3 用户可以使用该应用程序。
I call:
我打电话:
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
which is deprecated in iOS 4.0. I'm aware of this, and have measures in place to call the newer "withAnimation" version if we are running under iOS 4.0 or greater.
在 iOS 4.0 中已弃用。我知道这一点,如果我们在 iOS 4.0 或更高版本下运行,我会采取措施调用较新的“withAnimation”版本。
However, I'm getting a warning that I'm calling a deprecated SDK.
但是,我收到一条警告,提示我调用的是已弃用的 SDK。
I'd like to disable this specific warning in this specific place. I want all other warnings (including the same deprecated warning in other locations)
我想在这个特定的地方禁用这个特定的警告。我想要所有其他警告(包括其他位置的相同已弃用警告)
Can this be accomplished in Xcode?
这可以在Xcode中完成吗?
回答by Guillaume
For CLANG, this works:
对于 CLANG,这有效:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// Here I like to leave a comment to my future self to explain why I need this deprecated call
NSString *myUDID = [[UIDevice currentDevice] uniqueIdentifier];
#pragma clang diagnostic pop
You can use it inside a method, which allows you to be very specific about the line that causes the warning you want to have ignored.
您可以在方法中使用它,这使您可以非常具体地了解导致您想要忽略的警告的行。
回答by Will Ross
You might be able to use GCC pragmas. This should disable the deprecated warning for the enclosed function.
您也许可以使用 GCC 编译指示。这应该禁用封闭函数的弃用警告。
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
-(void)foo{
// As Georg Fritzsche notes below, the pragmas only work outside of functions
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
#pragma GCC diagnostic pop
I don't know if this will work with Clang, but it shouldwork with GCC.
我不知道这是否适用于 Clang,但它应该适用于 GCC。
Basically, it saves the state of the warnings/errors, disables the deprecated warning, compiles the function, then restores the state of the diagnostics.
基本上,它保存警告/错误的状态,禁用已弃用的警告,编译函数,然后恢复诊断状态。
回答by Georg Fritzsche
You can use NSInvocation
to get around the warnings independent of the compiler used:
您可以使用NSInvocation
独立于所使用的编译器来绕过警告:
UIApplication *app = [UIApplication sharedApplication];
SEL sel = @selector(setStatusBarHidden:animated:);
NSMethodSignature *sig = [app methodSignatureForSelector:sel];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
BOOL b = YES;
[inv setTarget:app];
[inv setSelector:sel];
[inv setArgument:&b atIndex:2];
[inv setArgument:&b atIndex:3];
[inv invoke];
Or in a less error-tolerant way:
或者以一种不太容错的方式:
UIApplication *app = [UIApplication sharedApplication];
SEL sel = @selector(setStatusBarHidden:animated:);
IMP imp = [app methodForSelector:sel];
imp(app, sel, YES, YES);
回答by anish
you can perform it like this to overcome the warnings at once
您可以像这样执行以立即克服警告
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
(void) methodUsingDeprecatedStuff { //use deprecated stuff }
or
或者
Just paste this line before using your deprecated stuffs every time to avoid warnings
在每次使用不推荐使用的东西之前粘贴这一行以避免警告
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
#pragma GCC 诊断警告“-Wdeprecated-declarations”
this will remove the warnings.
这将删除警告。
Hope it will help you.
希望它会帮助你。