Disabling a specific warning in a specific line in Xcode

14,715

Solution 1

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.

Solution 2

You might be able to use GCC pragmas. This should disable the deprecated warning for the enclosed function.

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
-(void)foo{
    // As Georg Fritzsche notes below, the pragmas only work outside of functions
    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
#pragma GCC diagnostic pop

I don't know if this will work with Clang, but it should work with GCC.

Basically, it saves the state of the warnings/errors, disables the deprecated warning, compiles the function, then restores the state of the diagnostics.

Solution 3

You can use NSInvocation to get around the warnings independent of the compiler used:

UIApplication *app = [UIApplication sharedApplication];
SEL sel = @selector(setStatusBarHidden:animated:);
NSMethodSignature *sig = [app methodSignatureForSelector:sel];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
BOOL b = YES;
[inv setTarget:app];
[inv setSelector:sel];
[inv setArgument:&b atIndex:2];
[inv setArgument:&b atIndex:3];
[inv invoke];

Or in a less error-tolerant way:

UIApplication *app = [UIApplication sharedApplication];
SEL sel = @selector(setStatusBarHidden:animated:);
IMP imp = [app methodForSelector:sel];
imp(app, sel, YES, YES);

Solution 4

you can perform it like this to overcome the warnings at once

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

or

Just paste this line before using your deprecated stuffs every time to avoid warnings

#pragma GCC diagnostic warning "-Wdeprecated-declarations"

this will remove the warnings.

Hope it will help you.

Share:
14,715
David Coufal
Author by

David Coufal

iOS Software Engineer

Updated on June 03, 2022

Comments

  • David Coufal
    David Coufal about 2 years

    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?

  • Georg Fritzsche
    Georg Fritzsche almost 14 years
    Those pragmas aren't allowed inside functions, they would have to surround some helper function.
  • paxswill
    paxswill almost 14 years
    Could the [NSMethodSignature signatureWithObjCTypes:"v@cc"] be replaced with [[UIApplication sharedApplication] methodSignatureForSelector:@selector(setStatusBarHidden)] to make it easier to read?
  • David Coufal
    David Coufal almost 14 years
    Thanks. This didn't actually work. It looks like the 'push' and 'pop' keywords are not valid, as they generated their own warnings.
  • user102008
    user102008 about 12 years
    or objc_msgSend(app, sel, YES, YES);
  • dwlz
    dwlz about 11 years
    This should really be marked as the correct answer. It's less hacky and solves the issue at compile time, in addition to being a bit cleaner.
  • Heavy_Bullets
    Heavy_Bullets almost 11 years
    +1 for correct usage of clang, this should be the answer, NSInvocation is a hack
  • Jakub
    Jakub over 9 years
    And also very nice for marking a comment for future self!
  • Leo Flaherty
    Leo Flaherty over 8 years
    I have a similar reason as David Coufal to want to suppress specific warnings. I get the general idea of how I'd apply this solution to my problem but if it would be possible, could I ask if there are any special caveats I need to be aware of when using NSInvocation?I get the impression that this would be very easy to use in the wrong context by accident.
  • lukas
    lukas about 7 years
    you really shouldn't NSInvocation or any other runtime APIs for this. Use [[UIApplication sharedApplication] performSelector:NSSelectorFromString(@"your_selector") withObject:arg1 withObject: arg2] instead
  • Adam Johns
    Adam Johns about 5 years
    See here for all warning ignore strings.
  • Elise van Looij
    Elise van Looij over 3 years
    And the list of warnings at clang LLVM: clang.llvm.org/docs/DiagnosticsReference.html