Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell the compiler that my class resolves methods dynamically?

I have a class which uses resolveInstanceMethod to dynamically implement methods. When I call the dynamically implemented methods from other parts of the code, the compiler emits a warning that the object may not respond to the selector. I would like the compiler to not emit such warnings for this class, but I don't want to suppress warnings when I invoke an invalid selector on other classes. Is this possible?

like image 929
Nick Moore Avatar asked Mar 02 '10 18:03

Nick Moore


2 Answers

Assuming you know the method signatures that will be dynamically resolved at compile time, you can declare 'em in an informal category:

@interface MyDynamicallyResolvingClass(MethodsThatWillResolveAtRuntime)
... declare the methods here ...
@end

No need to provide an implementation.

If you don't know the signatures -- if the method names are dynamic, too -- then you'll need to use either -performSelector: (or the single or double object argument variants) or you will likely want to use NSInvocation, unless performance is a major concern (if it is, there are alternative solutions that are significantly more fiddly).

like image 115
bbum Avatar answered Nov 15 '22 04:11

bbum


Use performSelector:

It's equivalent to sending a message directly to the receiver, however, it allows you to send messages that aren’t determined until runtime.

If your methods take one or two arguments, you can use the sisters of this method: – performSelector:withObject: and – performSelector:withObject:withObject:

If your methods have more than two arguments, or arguments that are not of object type, this answer is not adapted.

like image 20
Guillaume Avatar answered Nov 15 '22 05:11

Guillaume