140
votes

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?

9
While Paul R's answer works, consider that manicaesar is a bit more surgical, in that it allows you to suppress exactly the warning you want, without losing other additional warnings which might be important. It seems to me that, in terms of best-practices, manicaesar has The Correct Answer™Olie

9 Answers

339
votes

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
147
votes

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.

85
votes

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

44
votes

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.

29
votes

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.

23
votes

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

8
votes

In your build settings, find Deprecated Functions.

enter image description here

4
votes

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
-3
votes

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

#pragma clang system_header