Saturday, 31 August 2013

How to send not declared selector without performSelector:?

How to send not declared selector without performSelector:?

Background: I have an object (let's call it BackendClient) that represents
connection with server. Its methods are generated to single @protocol and
they are all synchronous, so I want to create proxy object that will call
them in background. The main problem is return value, which I obviously
can't return from async method, so I need to pass a callback. The "easy"
way will be copy all BackendClient's methods and add callback argument.
But that's not very dynamic way of solving that problem, while ObjectiveC
nature is dynamic. That's where performSelector: appears. It solves
problem entirely, but it almost kills proxy object transparency.
Problem: I want to be able to send not declared selector to proxy
(subclass of NSProxy) object as if it was already declared. For example, I
have method:
-(AuthResponse)authByRequest:(AuthRequest*)request
in BackendClient protocol. And I want proxy call look like this:
[proxyClient authByRequest:myRequest withCallback:myCallback];
But this wouldn't compile because
No visible @interface for 'BackendClientProxy' declares the selector
'authByRequest:withCallBack:'
OK. Let's calm down compiler a bit:
[(id)proxyClient authByRequest:myRequest withCallback:myCallback];
Awww. Another error:
No known instance method for selector 'authByRequest:withCallBack:'
The only thing that comes to my mind and this point is somehow construct
new @protocol with needed methods at runtime, but I have no idea how to do
that.
Conclusion: I need to suppress this compilation error. Any idea how to do
that?

No comments:

Post a Comment