开发者

Xcode/iOS -- get rid of deprecation warnings for specific constants?

开发者 https://www.devze.com 2023-03-15 05:35 出处:网络
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 proje

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


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.


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.


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.


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

    // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号