我知道这是一个旧话题,但今天我也碰到了同样的烦恼。
例如:你想摆脱令人讨厌的“弃用警告”,但只针对 [[UIDevice currentDevice] uniqueIdentifier]]
,因为你很可能想在使用 TestFlight 进行开发阶段时使用它。
如果您错误地使用其他已弃用的声明,仍需要编译器提醒您。
我喜欢sarfata的答案:它能够完成工作。 但是还有一种更加“政治正确”的方法可用:
以下方法来自The Goo Software Blog。
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[TestFlight setDeviceIdentifier:[[UIDevice currentDevice] uniqueIdentifier]];
#pragma clang diagnostic pop
在构建发布版之前,请确保注释掉这些行。或者简单地使用预处理器宏来从发布版本中排除这些行。
添加到编译器标志:
-Wno-deprecated-declarations
Warn About Deprecated Functions
如果你查看构建输出(在Xcode 4中按Apple+7),你会注意到前面提到的编译器标志。
这是谷歌搜索排名第一的答案,我相信在某些情况下使用已弃用的方法是有用的,例如当您想要避免警告以保持构建“干净”时。此解决方案的灵感来自:http://vgable.com/blog/2009/06/15/ignoring-just-one-deprecated-warning/
思路是声明一个具有相同方法(但当然不是已弃用的)的新协议,并将对象转换为该协议。这样,您可以调用该方法而不会得到警告,并且无需摆脱所有已弃用的警告。
例如:如果您想将TestFlight集成到应用程序中,SDK文档建议在测试版中传输设备的唯一标识符。这可以帮助跟踪哪个测试人员遇到了问题。尽管苹果已经弃用了此方法(并且他们不会让您提交应用程序),但我认为这是使用已弃用的方法的好例子。
在您的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]];
// ...
}