Suppressing deprecated warnings in Xcode

IphoneXcodeMacosWarnings

Iphone Problem Overview


With all the SDKs floating around, it's handy to be able to build for multiple SDKs and platforms. However, bouncing from 3.2 to 3.0 and even occasionally 2.x, I frequently get deprecated warnings involving methods that have changed or been superseded:

warning: 'UIKeyboardBoundsUserInfoKey' is deprecated.

Since I still want to maintain compatibility with older OSes, and I'm also striving to remove 'noise' when building, is there a way to turn off or disable these warnings?

Iphone Solutions


Solution 1 - Iphone

Since I yet can not add a comment to the @samiq post, I think I will expand it. Input mentioned directive before a function / method in which you use deprecated stuff. Then you can restore the previous setting after the definition of the function end:

#pragma GCC diagnostic push 
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
- (void) methodUsingDeprecatedStuff {
    //use deprecated stuff
}
#pragma GCC diagnostic pop

Solution 2 - Iphone

Clang provides a nice feature that makes the "restore" step in the @manicaesar post independent of the initial warning state:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void) methodUsingDeprecatedStuff {
    //use deprecated stuff
}
#pragma clang diagnostic pop

To quote the Clang manual:

> In addition to all of the functionality provided by GCC's pragma, Clang also allows you to push and pop the current warning state. This is particularly useful when writing a header file that will be compiled by other people, because you don't know what warning flags they build with.

Solution 3 - Iphone

Try -Wno-deprecated-declarations, or its corresponding setting in Xcode, GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS (pro tip: just type in "deprecated" in the build settings to find the specific setting for this warning).

Current versions of Xcode (e.g. Xcode 9.2):

enter image description here


Ancient versions of Xcode (e.g. Xcode 2.x, 3.x):

enter image description here

Solution 4 - Iphone

Since we tend to need to support older OSes, but pay attention to our warnings, I wanted a tidier way to do this. I put this together, inspired by some Mozilla code:

#define SILENCE_DEPRECATION(expr)                                   \
do {                                                                \
_Pragma("clang diagnostic push")                                    \
_Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")   \
expr;                                                               \
_Pragma("clang diagnostic pop")                                     \
} while(0)

#define SILENCE_IOS7_DEPRECATION(expr) SILENCE_DEPRECATION(expr)
#define SILENCE_IOS8_DEPRECATION(expr) SILENCE_DEPRECATION(expr)

This allows you to do the following:

SILENCE_IOS7_DEPRECATION(return [self sizeWithFont:font constrainedToSize:size]);

It also works with blocks of code:

SILENCE_IOS7_DEPRECATION(
    view = [[MKPolylineView alloc] initWithPolyline:self];
    view.lineWidth = self.lineWidth;
    view.strokeColor = self.color;
);

Also, when you do drop support for pre-iOS 7 devices, you can easily search through the code to find the deprecated usages to fix.

Solution 5 - Iphone

You can also suppress warnings per file by using

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

which in turn makes it a little bit better practice than just suppressing all warning once and together... after all you got to know what you are doing it for.

Solution 6 - Iphone

If you want to silence warning Implementing deprecated method or Implementing deprecated class, use:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
// code
#pragma clang diagnostic pop

Solution 7 - Iphone

In your build settings, find Deprecated Functions.

enter image description here

Solution 8 - Iphone

If you would like a blanket check for all kinds of deprecations in a piece of code. Please use the -Wdeprecated flag like below:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
- (void) methodUsingDeprecatedStuff {
    //use deprecated stuff
}
#pragma clang diagnostic pop

Solution 9 - Iphone

To disable warning from third-party header file, add following line at the top of file

#pragma clang system_header

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBen GottliebView Question on Stackoverflow
Solution 1 - IphonemanicaesarView Answer on Stackoverflow
Solution 2 - IphoneAndrew HershbergerView Answer on Stackoverflow
Solution 3 - IphonePaul RView Answer on Stackoverflow
Solution 4 - IphoneJoe TrellickView Answer on Stackoverflow
Solution 5 - IphonesamiqView Answer on Stackoverflow
Solution 6 - IphonekrzysztofView Answer on Stackoverflow
Solution 7 - IphoneSmallChessView Answer on Stackoverflow
Solution 8 - IphonejaroraView Answer on Stackoverflow
Solution 9 - IphoneharvestliView Answer on Stackoverflow