How do I temporary disable warnings in my objectiv

2019-04-07 23:23发布

One project I work on has a build system that enforces no warnings.

But I have some code that needs warnings to work. Here is an example

NSString* title = @"";
if ([view respondsToSelector:@selector(title)]) {
  title = [view title];
}

After some googling I tried disable warnings for the code block by wrapping that code area with

#pragma warning disable
// my code
#pragma warning restore

Didn't work :(

Anyone know how to do this in Xcode?

Any help is appreciated.

-CV

3条回答
神经病院院长
2楼-- · 2019-04-07 23:36

There are a number of things you could do here, but the simplest would probably to rewrite your code just a tad.

NSString* title = @"";

if ([view respondsToSelector:@selector(title)]) {
  title = [(id)view title];
}

Casting the view variable to id before sending the message should ensure that so long as a method named -title exists anywhere, it'll stay silent.

Another option:

NSString* title = @"";

if ([view respondsToSelector:@selector(title)]) {
  title = [view performSelector:@selector(title)];
}

This is a little different from the above, in that it doesn't require the file to "see" any method named title; but it's a bit more wordy.

Edit: I'm aware that neither of these approaches actually turn of warnings for any amount of time, but rather suppress them.

Suppression, when done correctly, at least, is usually better than simply ignoring.

查看更多
时光不老,我们不散
3楼-- · 2019-04-07 23:54
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow-ivar"
// your code
#pragma GCC diagnostic pop

You can learn about GCC pragma here and to get the warning code of a warning go to the Log Navigator (Command+7), select the topmost build, expand the log (the '=' button on the right), and scroll to the bottom and there your warning code is within square brackets like this [-Wshadow-ivar]


Edit

For clang you can use

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow-ivar"
// your code
#pragma clang diagnostic pop
查看更多
神经病院院长
4楼-- · 2019-04-07 23:57

The closest way to do what you want is to use the GCC diagnostic pragma. See this question for details.

查看更多
登录 后发表回答