iphonexcodewarningssuppress-warnings

Disabling a specific warning in a specific line in Xcode


I'm writing an iPhone app against the Base 4.0 SDK, but I'm targeting OS 3.1.3 so OS 3 users can use the app.

I call:

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];

which is deprecated in iOS 4.0. I'm aware of this, and have measures in place to call the newer "withAnimation" version if we are running under iOS 4.0 or greater.

However, I'm getting a warning that I'm calling a deprecated SDK.

I'd like to disable this specific warning in this specific place. I want all other warnings (including the same deprecated warning in other locations)

Can this be accomplished in Xcode?


Solution

  • For CLANG, this works:

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
      // Here I like to leave a comment to my future self to explain why I need this deprecated call
      NSString *myUDID = [[UIDevice currentDevice] uniqueIdentifier];
    #pragma clang diagnostic pop
    

    You can use it inside a method, which allows you to be very specific about the line that causes the warning you want to have ignored.


    For suppressing any warnings, not just your specific deprecation warning, update these preprocessors to instead use the `-Weverything` flag.

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Weverything"
      /*
        Any section of code which contains warnings you wish to suppress
      */
    #pragma clang diagnostic pop
    

    If you want to suppress just a specific type of warning, instead of using `-Weverything`, you need to figure out what is the specific flag to use in the pragma preprocessor for that specific type of warning.

    To do so, simply right click on the specific warning in xCode, click 'reveal in logs', and in the logs you will see the specific flag for that type of warning (e.g. -Wdeprecated-declarations).

    To suppress multiple specific types of warnings at once, you can simply repeat the #pragma clang diagnostic ignored line multiple times in a row with different flags in each instance, all within a single push/pop wrapper.


    If you wish to suppress a specific type of warning project-wide, instead of just around a specific section using pragma preprocessors, go to your project settings, search for 'Other Linker Flags' and add the flag of the warning you wish to suppress, but with "no-" inserted after the "-W".

    So if the type of warning's flag was -Waaa-bbb then you would use -Wno-aaa-bbb in the 'Other Linker Flags' area. This will suppress the warnings of that nature across your entire project. (Note: sometimes it can take a minute for xCode to update the warnings, you may have to quit and re-open xCode).