Xcode/iOS——摆脱特定常量的弃用警告?

发布于 2024-11-17 13:34:07 字数 544 浏览 0 评论 0原文

我的项目中有一些已弃用的常量。他们需要留下来。我不想收到关于它们的警告,但是如果稍后在我的项目中出现其他已弃用的常量,我确实希望收到警告。

Apple 的标头将它们声明如下:

extern NSString * const NameOfStringConstant __OSX_AVAILABLE_BUT_DEPRECATED(version availability info here)

如何使警告静音?

有关消除已弃用方法警告的相关答案此处
有关消除有关已弃用字符串转换的警告的相关答案 此处

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

挽清梦 2024-11-24 13:34:07

我知道这是一个老话题,但今天我也遇到了同样的烦恼。

示例:您希望摆脱烦人的弃用警告,但仅针对 [[UIDevice currentDevice] uniqueIdentifier]] 因为您很可能希望在 TestFlight 的开发阶段使用它。
如果您错误地使用了其他一些已弃用的声明,您仍然希望编译器发出警告。

我喜欢 sarfata 的答案:它可以完成任务。但还有更多政治正确的方法:

以下配方取自Goo 软件博客

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [TestFlight setDeviceIdentifier:[[UIDevice currentDevice] uniqueIdentifier]];
#pragma clang diagnostic pop

确保在构建分发之前注释掉此行。或者简单地使用预处理器宏从发布版本中排除此行。

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.

I like sarfata's answer: it does the job. But there's more politically correct way available:

Following recipe is taken from 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.

梅倚清风 2024-11-24 13:34:07

添加到编译器标志:

-Wno-deprecated-declarations

或者,在 Xcode 中,为构建设置选项选择“否”:

Warn About Deprecated Functions

然后如果您查看构建输出(Xcode 4 中的 Apple+7),您会注意到上述编译器标志。

Add to the compiler flags:

-Wno-deprecated-declarations

or, in Xcode, select "No" for the build setting option:

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.

你的笑 2024-11-24 13:34:07

这个问题的正确答案是不使用已弃用的常量。检查文档以获取立即完成某些任务的推荐方法。对于已弃用的方法/常量/无论什么,如果您愿意,几乎总是有一个指向“替换”的链接。用它来代替。这样,当这些永远消失时,您的代码不会神秘地中断,但您的用户仍然拥有针对旧 sdk 构建的版本,现在他们的代码崩溃了,或者更糟的是,做了奇怪的事情。

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.

戒ㄋ 2024-11-24 13:34:07

这是谷歌中的#1答案,我相信在使用已弃用的方法有用并且您想避免警告以保持构建“干净”时,有一些公平的情况。该解决方案的灵感来自:http://vgable。 com/blog/2009/06/15/ignoring-just-one-deprecated-warning/

这个想法是声明一个具有相同方法的新协议(但不弃用当然)并将对象强制转换为该协议。这样您就可以调用该方法而不会收到警告,也不会消除所有弃用警告。

示例:如果您想将 TestFlight 集成到您的应用程序中,SDK 文档建议在 BETA 中传输设备的唯一标识符。这可以帮助跟踪哪个测试人员出现了问题。 Apple 已弃用此方法(并且他们不会让您提交应用程序),但我相信这是使用已弃用方法的一个很好的示例。

在您的应用程序委托中:

/* 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]];

    // ...
}

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/

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.

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]];

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