Reputation: 18237
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
Upvotes: 7
Views: 5320
Reputation: 39978
#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
Upvotes: 12
Reputation: 27889
The closest way to do what you want is to use the GCC diagnostic pragma. See this question for details.
Upvotes: 1
Reputation: 28999
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.
Upvotes: 2