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

I have a class that uses resolveInstanceMethod to dynamically implement methods. When I call dynamically implemented methods from other parts of the code, the compiler gives a warning that the object may not respond to the selector. I would like the compiler not to issue such warnings for this class, but I do not want to suppress warnings when I call an invalid selector for other classes. Is it possible?

+6
objective-c cocoa
source share
2 answers

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

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

There is no need to provide an implementation.

If you don’t know the signature - if the method names are also dynamic, then you will need to use either -performSelector: (or options for the arguments of one or two objects), or you most likely want to use NSInvocation if performance is not a serious problem (if there is , there are alternative solutions that are significantly more difficult).

+7
source share

Use performSelector:

This is equivalent to sending a message directly to the recipient, but it allows you to send messages that were determined before the execution time.

If your methods accept 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 the type of an object, this answer is not adaptable.

+4
source share

All Articles