views:

366

answers:

2

I've got a call like this [Class method] and a warning saying that Class may not respond to "method" message.

The "method" message does not indeed exist but my code use unknown message catching (using forwardingTargetForSelector) so it will run fine and it is build to run that way. How can I hide this annoying warning ?

A: 

You could define a category that declares that method. Having the definition in scope at the time of compilation would avoid the warning. Something like

@interface MyClass (ShutUpTheCompilerMethods)
- (void)method;
@end

...

[MyClass method] //no warning here
Barry Wark
+2  A: 

If you intend to send a possibly-unimplemented message to an object, and you know that you're going to catch failures, you should use the invocation:

id myClone = [anObject performSelector:@selector(copy)];

That declares your intent more directly that you are calling a method that may not exist, and you're cool with that. This is a more clear way to do it than to suppress the warning or fake out the method.

cdespinosa
Thanks.I guess it would be less efficient though if I have to create a selector at each call. Unless I declare all my @selector one and for all.
CodeFlakes
The performance difference will be virtually undetectable. If you're worried, profile it. Don't prematurely optimize.
Rob Keniger

related questions