Disabling a specific warning in a specific line in Xcode Disabling a specific warning in a specific line in Xcode xcode xcode

Disabling a specific warning in a specific line in Xcode


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.


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.


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