Is there a way to suppress warnings in Xcode? - objective-c

Is there a way to suppress warnings in Xcode?
For example I am calling an undocumented method and since the method is not in the header I get a warning on compile. I know I can add it to my header to stop the warning, but I am wondering if there is a way other than adding it to the header (so I can keep the headers clean and standard) to suppress the warning? A pragma or something?

To disable warnings on a per-file basis, using Xcode 3 and llvm-gcc-4.2 you can use:
#pragma GCC diagnostic ignored "-Wwarning-flag"
Where warning name is some gcc warning flag.
This overrides any warning flags on the command line. It doesn't work with all warnings though. Add -fdiagnostics-show-option to your CFLAGS and you can see which flag you can use to disable that warning.

there is a simpler way to suppress Unused variable warnings:
#pragma unused(varname)
EDIT:
source: http://www.cocoadev.com/index.pl?XCodePragmas
UPDATE:
I came accross with a new solution, a more robust one
Open the Project > Edit Active Target> Build tab.
Under User-Defined: find (or create if you don't find one )the key : GCC_WARN_UNUSED_VARIABLE set it to NO.
EDIT-2
Example:
BOOL ok = YES;
NSAssert1(ok, #"Failed to calculate the first day the month based on %#", self);
the compiler shows unused variable warning for ok.
Solution:
BOOL ok = YES;
#pragma unused(ok)
NSAssert1(ok, #"Failed to calculate the first day the month based on %#", self);
PS:
You can also set/reset other warning:
GCC_WARN_ABOUT_RETURN_TYPE : YES/NO

For gcc you can use
#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 Report Navigator (Command+9), 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]
For clang you can use
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow-ivar"
// your code
#pragma clang diagnostic pop

In order to surpress a warning for an individual file do the following:
select the file in the xcode project.
press get info
go to the page with build options
enter -Wno- to negate a warning:
-Wno-
e.g.
-Wno-unused-parameter
You can get the name of the warning if you look on the project settings look at the GCC warnings located at the bottom of the build tab page, by clicking on each warning it will tell you the warning parameter name:
e.g.
Warn whenever a function parameter is
unused aside from its declaration.
[GCC_WARN_UNUSED_PARAMETER,
-Wunused-parameter]

With Objective-C, a number of serious errors only appear as warnings. Not only do I never disable warnings, I normally turn on "Treat warnings as errors" (-Werror).
Every type of warning in your code can be avoided by doing things correctly (normally by casting objects to the correct type) or by declaring prototypes when you need them.

To get rid of the warning: try creating a category interface for the object in question
#interface NSTheClass (MyUndocumentedMethodsForNSTheClass)
-(id)theUndocumentedMethod;
#end
...
#implementation myClass : mySuperclass
-(void) myMethod {
...
[theObject theUndocumentedMethod];
...
}
As an aside, I strongly advise against calling undocumented methods in shipping code. The interface can and will change, and it will be your fault.

http://nshipster.com/pragma/#inhibiting-warnings - skip to inhibiting warnings section

Create a new, separate header file called 'Undocumented.h' and add it to your project. Then create one interface block for each class you want to call undocumented functions on and give each a category of '(Undocumented)'. Then just include that one header file in your PCH. This way your original header files remain clean, there's only one other file to maintain, and you can comment out one line in your PCH to re-enable all the warnings again.
I also use this method for depreciated functions in 'Depreciated.h' with a category of '(Depreciated)'.
the best part is you can selectively enable/disable individual warnings by commenting or uncommenting the individual prototypes.

Suppressing that particular warning is not safe. The compiler needs to know the types of the arguments and returns to a method to generate correct code.
For example, if you're calling a method like this
[foo doSomethingWithFloat:1.0];
that takes a float, and there is no prototype visible, then the compiler will guess that the method takes a double, not a float. This can cause crashes and incorrectly interpreted values. In the example above, on a little endian machine like the intel machines, the receiver method would see 0 passed, not 1.
You can read why in the i386 ABI docs, or you can just fix your warnings. :-)

Related

Mute NSAssert warning

I compiled my project with -Weverything and I see that all the NSAssert calls have the warning
must specify at least one argument for '...' parameter of variadic macro
NSAssert(NO, #"This class cannot be instantiated!");
The correct fix would be to add a nil argument to all statements but I would prefer just to mute this warning. Is it possible?
cheers
Add the following parameters to your Other C flags in XCode build settings which turns off all NSAssert in your project:
-DNS_BLOCK_ASSERTIONS=1

Suppress -Wproperty-no-attribute not working in Xcode

In an Xcode 5, ARC, iPhone project, I have received a large number of warnings reading "No 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed." I know what this warning means, and why I might want to fix it. But this would take a long time given the size of my project, so I instead want to suppress the warning. However, setting the -Wno-property-no-attribute flag on all of my .m files didn't fix anything, nor did setting the flag at Custom Compiler Flags > Other Warning Flags. I also tried #pragma GCC/clang diagnostic ignored "-Wproperty-no-attribute" before my property declarations, and have cleaned my build folder many times. The warnings persist. (Also, the -w flag did not work to suppress all warnings, when applied to my .m files.)
What should I do?
The pragma statement should be this: #pragma clang diagnostic ignored "-Wobjc-property-no-attribute"
It turned out that I had made a small typo in the Custom Compiler Flags entry (although I still do not know why the file-per-file flags didn't work), and that the LLVM warning about this was almost invisible, being swamped in the hundreds of other warnings generated.

Ignore "Unused Entity Issue: Unused Variable" in a single file

I want to get rid of this compiler warning in just one file of my Xcode project. Is there a way to do this?
You can turn off specfic warnings in Clang using a pragma directive and the "diagnostic" keyword, like so:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
// Insert code here
#pragma clang diagnostic pop
No unused variable warnings will be produced for the code between the push and the pop.
A second option, even more targeted, is to mark a particular variable with a GCC-style attribute, specifically, "unused". Clang honors GCC's established attributes and won't issue a warning about that one variable:
__attribute__((unused))
NSString * thisStringIsJustForFun = #"It's only work if somebody makes you do it.";

sending messages to Class - get rid of "multiple methods named [...]" warning

I am passing a Class type as a parameter to a method, and the LLVM compiler in XCode 4.5.2 generates a warning, "multiple methods named 'foo' found", which is understandable, but undesired in this case. How do I get rid of this warning, either by disabling this type of warning, or by making changes to my code?
- (void) fooWithClass: (Class) theClass
{
[theClass aClassMethodOfThatClass];
}
more specifically, the parameter is a subclass of a certain base class, and i am sending a message which is declared in that base class.
Deactivating -Wall did not work for me, in case anybody is still interested:
After digging around in the LLVM manual, I found that the exact warning you need to ignore using Peres's method (as of Xcode 7) is:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-selector-match"
// code
#pragma GCC diagnostic pop
To disable the warning, you can check the clang manual. Like this:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wall"
[theClass aClassMethodOfThatClass];
#pragma GCC diagnostic pop

Implicit declaration of function - C99

I am currently using Xcode 4, and in my .pch file I have this macro:
#define localize(s) NSLocalizedString((s), nil).
When I try to use this macro in some .m file, I receive this warning: Implicit declaration of function 'localize' is invalid in C99.
This code compiles without a problem, but how can I fix this so I don't get a warning?
I had this problem when I did a global replace of NSLog with DLog. I foolishly included the
#define DLog(...) NSLog(...
statements, so I ended up with
#define DLog(...) DLog(...
which caused the warnings, and a linker error.
Implicit function declarations are those that the compiler sees the first time used as a function call (as opposed to those where a prototype or the function definition is seen first).
Apparently your code used localize(foo) but the macro definition was not visible. Possible reasons: you forgot to #include the file containing the localize macro or the precompilation of headers went south an did not include the localize macro so it was left unexpanded.
Another "foolish" mistake I ran into was the fact that my DLog was defined in the prefix header of the iOS target, so I had to copy it over to the prefix of the OSX target, as well...
I had this problem because I accidentally imported CocoaLumberjack like this:
#import <CocoaLumberjack/DDLog.h>
Apparently the CocoaLumberjack team modularized the code some more; and macros like DDLogError are now defined separately in their own header file.
I replaced the import statement with this and the error went away:
#import <CocoaLumberjack/CocoaLumberjack.h>
In my case only one file was giving this error. Turned out that I added it to the project's tests target membership (in the File Inspector on the right).