views:

4546

answers:

8

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 then adding it to the header (so I can keep the headers clean and standard) to suppress the warning? A pragma or something?

+12  A: 

Write the prototype. Not in the header, but in the file where you need it. You should not disable compiler warnings when you have the option of writing correct code.

Terminus
err.. I forgot all about prototypes when I wrote the question. Thanks
kdbdallas
+1  A: 

XCode users GCC, so whatever works for GCC will work for XCode too.

AFAIK GCC doesn't have pragmas for suppressing warnings, only command line switches. You can control these in XCode via Project Settings → Build tab.

porneL
+10  A: 

To diable 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.

robottobor
+3  A: 

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. :-)

Ken
+6  A: 

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.

Matt Gallagher
+2  A: 

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.

Mark Pauley
I do this as well. I call my category "Private" and put it at the top of the .m file... It serves as a way to forward declare the methods that are only used within the file. I agree that a private header file would be more standard, but having to constantly bounce between files for something that really should be wholly contained (private) to the implementation is annoying.
Pat Niemeyer
+1  A: 

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.

MarqueIV
A: 

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]

Anders K.