8

Similar to Ben Gottlieb's question, I have a handful of deprecated calls that are bugging me. Is there a way to suppress warnings by line? For instance:

 if([[UIApplication sharedApplication]
  respondsToSelector:@selector(setStatusBarHidden:withAnimation:)]) {

  [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
 } else {
  [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO]; //causes deprecation warning
 }

All I care about is that line. I don't want to turn off all deprecation warnings. I would also rather not do something like suppress specific warnings by file.

There have been a few other circumstances where I wanted to flag a specific line as okay even though the compiler generates a warning. I essentially want to let my team know that the problem has been handled and stop getting bugged about the same line over and over.

Community's user avatar

asked May 17, 2010 at 22:00

MrHen's user avatar

5

Vincent Gable has posted an interesting solution. In short:

@protocol UIApplicationDeprecatedMethods
- (void)setStatusBarHidden:(BOOL)hidden animated:(BOOL)animated;
@end

if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden:withAnimation:)]) {
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; 
} else { 
    id<UIApplicationDeprecatedMethods> app = [UIApplication sharedApplication];
    [app setStatusBarHidden:YES animated:NO];
}

answered May 18, 2010 at 0:20

mbauman's user avatar

7 Comments

Cool. I guess that works for dodging the deprecation warnings. The more abstract question is still open, however. Is there a way to suppress a specific warning in XCode?

Unfortunately, it's mostly all or nothing. Through the use of #pragma GCC diagnostic ... (gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html), you can disable a specific warning on a per-file basis in a more obvious way than the per-file build settings. It does require GCC 4.2+, and must be placed at the very top of a translation unit.

@matt-b FYI, I think I found a better way to deal with the deprecation warnings. If I cast UIApplication to (id) the error goes away. Can you think of a reason this is improper?

@MrHen: Yes, that does sometimes work. When you send id a selector, the compiler checks to ensure that at least some class is able to respond to such a selector. That may not always be the case with a deprecated selector. If not, a no method found warning is raised. Thus, adding a protocol is the more robust solution; it will always circumvent the warning.

This works for me, but I do have to make one change. My code reads: id<UIApplicationDeprecatedMethods> app = (id)[UIApplication sharedApplication]; Otherwise that line gives me a warning.

|

0

if([[UIApplication sharedApplication]
  respondsToSelector:@selector(setStatusBarHidden:withAnimation:)]) {

  [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
 } else {
  [(id)[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
 }

answered Apr 24, 2012 at 20:57

user102008's user avatar

user102008

31.6k13 gold badges85 silver badges106 bronze badges

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.