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

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

Related

Suppressing deprecation warning in Objective-C with #available

Given the #available command in Objective-C, I was expecting that the deprecation warning would go away just like in Swift.
Example:
ABPerson *abPerson = [participant ABPersonInAddressBook:[ABAddressBook addressBook]];
'meetingAttendeeWithMeetingPlanner:andABPerson:' is deprecated: first deprecated in macOS 10.11 - use contact framework
With the #available close I can condition the use of the api framework, my guess is that this would silence the warning but it doesn't. Is this the right way to use the #available command, and if it is, is there a way to silence the warning?
if (#available(iOS 9, macOS 10.11, *)) {
}
else{
ABPerson *abPerson = [participant ABPersonInAddressBook:[ABAddressBook addressBook]];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
... the code using the deprecated API ...
#pragma clang diagnostic pop
#available in Objective-C is used when you want to check, if current iOS (/MacOS) version supports this API. It is called when you implement API which is supported starting by some iOS version, but not supported by older iOS.
As far as I know, you can't silent warning about deprecated API - you can only replace it with new.

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.";

How to suppress the deprecation warning "dispatch_get_current_queue() is deprecated...in iOS 6.0"?

So the question is the subject question - I want to get rid of this warning which is pretty annoying.
Is there a way to make it silent?
Note: I use dispatch_get_current_queue() for debugging purposes only.
You could use the following code to suppress the warnings.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
dispatch_get_current_queue() // your deprecated calling code
#pragma clang diagnostic pop

Is there a way to suppress warnings in Xcode?

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