Deprecated Warnings in Xcode and How to Handle Deprecation

deprecated warnings in xcode and how to handle deprecation

  1. Apple is unaware of any compile-time warnings you receive with your code.

  2. Yes, you are handling this practice correctly. Clearly, in this case, you only need to go through this effort if you're supporting iOS prior to 5.0. But, in general, the technique for testing whether a method can be invoked and then calling the appropriate rendition is absolutely correct.

  3. If you want to turn off the warning, you would simply temporarily suppress the warning and then turn it back on afterwards using the appropriate #pragma syntax:

    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
    {
    //post-iOS6.0
    [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
    // pre-iOS6.0
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [self dismissModalViewControllerAnimated:YES];
    #pragma clang diagnostic pop
    }

    By the way, if you want to know what the -W code is for your particular warning, go to your Log Navigator, select the recent build that included the warning, and expand the log, and you'll see it there:

    Sample Image

Also note that while you could suppress the warning like I've illustrated above, in practice, you rarely need to do so. Using your example, if your project's iOS deployment target was 4.3, you wouldn't get the warning. And if your deployment target was 6.0 or higher, you'd get that warning, but then again, you probably wouldn't need this conditional code to call dismissModalViewControllerAnimated because effective iOS 5.0, you can always use dismissViewControllerAnimated.

The only time that you'd need to suppress this warning in your code is if you have source code, to be included in projects in the future, for which you don't know what the deployment target will be. Using your example, if you don't know whether the above code will be included in a project with a 4.3 deployment target or a 5.0+ deployment target. In that case, this syntax is quite useful. But, then again, I you could also use conditional checks on __IPHONE_OS_VERSION_MIN_REQUIRED, e.g.:

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
{
//post-iOS5.0
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
// pre-iOS5.0
[self dismissModalViewControllerAnimated:YES];
}
#else
[self dismissViewControllerAnimated:YES completion:nil];
#endif

Suppressing deprecated warnings in Xcode

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):

Sample Image


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

Sample Image

Suppress `deprecated` warnings in Xcode

If I am correct, you simply want to suppress the warnings.

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

This is simply to suppress the warnings. In release builds, you should not use any deprecated functions.

EDIT: To suppress specific code that invokes warnings, use :

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

[self dismissModalViewControllerAnimated:YES];

#pragma clang diagnostic pop

Xcode no longer showing me deprecated warnings

I realize this is a pretty old thread, but I thought I'd post an answer in case others find this thread.

It also depends on the Deployment Target setting of the Target (i.e., under "TARGETS", click the app and choose "General" at the top, look in the "Deployment Info" section for "Deployment Target"). For example, if I have the Deployment Target at 7.1 and use functions that were deprecated in iOS 8, I won't get warnings. However, if I set the Deployment Target to 8.x or 9.0, I will get warnings for those functions.



Related Topics



Leave a reply



Submit