Re: NSInvocation
Re: NSInvocation
- Subject: Re: NSInvocation
- From: "Michael Ash" <email@hidden>
- Date: Wed, 10 Sep 2008 10:52:30 -0400
On Wed, Sep 10, 2008 at 2:56 AM, Chris Idou <email@hidden> wrote:
>
> I have a need to call performSelector:withObject etc, except I need to pass 3 arguments. The doco to performSelector:withObject:withObject says to "See NSInvocation", which I have done, but I don't understand how to use it. Can anyone give me some code which implements performSelector:withObject etc as example?
Ignore the docs. NSInvocation is inconveniently difficult and slow.
Instead use methodForSelector:. This gives you a function pointer
which you can then call easily using C.
How to do this is probably not very obvious, so I'll give an example
(written in mail client, beware, etc.):
// using typedefs with function pointers makes life vastly easier
// first two arguments of any ObjC method are *always*
// id and SEL
typedef id (*ThreeObjectArgumentsFptr)(id self, SEL _cmd, id, id, id);
SEL sel = @selector(thisTakes:three:arguments:);
ThreeObjectArgumentsFptr fptr = (ThreeObjectArgumentsFptr)[obj
methodForSelector:sel];
id retval = fptr(obj, sel, arg1, arg2, arg3);
I'm assuming the method returns id since that's what performSelector:
assumes. If it returns a different type, just change the return type
declaration in the typedef. If it returns void then you can get away
with leaving it as 'id' and ignoring the result, like performSelector:
does, but I really don't recommend that.
And lastly, I recommend filing a bug against the documentation in this
case. NSInvocation is not really suited for this particular task, and
I don't understand why they would recommend it here.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden