Re: NSInvocation question
Re: NSInvocation question
- Subject: Re: NSInvocation question
- From: Hank Heijink <email@hidden>
- Date: Mon, 25 Feb 2008 09:49:22 -0500
On Feb 22, 2008, at 8:47 PM, Chris Suter wrote:
On 23/02/2008, at 2:36 AM, Hank Heijink wrote:
The nice thing about NSInvocation is not so much its efficiency (or
lack thereof, as the case may be), but the fact that once you've
got the thing constructed, you can just call invoke on it, without
having to think about what's inside. I have functions of zero, one,
or two arguments that I wrapped up in NSInvocations. With IMPs, I
have to check the number of arguments both for the typing and for
the call.
However, even with the check for number of arguments, an IMP is
very very fast.
What you say here doesn't make sense to me. Without knowing exactly
what you're trying to do, it's difficult for me to comment. You can
arrange things so that it's simple without using NSInvocations. It
sounds like you've got some kind of delegate pattern, so the usual
Cocoa way of doing this is to pass a selector and a target around.
For example:
[examples deleted]
I haven't been very clear, my apologies. I may have completely
overlooked the best strategy, so let me try to explain what I'm doing.
I have to call methods depending on certain conditions. These include
passing of time, movement of the cursor, speed of the cursor, etc.
All those methods are on MyDocument. For example:
- (void)makeGraphic:(MVGraphic *)aGraphic changeStatusTo:(NSNumber
*)newStatus;
- (void)startTrial:(MVTrial *)trial;
The reason I liked NSInvocations is that just before running the
experiment, I could wrap the selector and the arguments in an
invocation and when running the experiment, I could call -[invoke] on
both without knowing anything about which method is being called and
how many arguments it has.
Assume I have an object with the arguments as instance variables. When
I'm using an IMP or -[performSelector] variants, I seem to need
something like this:
switch (numberOfArguments) {
case 0:
// callBackAsImp has type void (*)(id, SEL)
callBackAsImp();
break;
case 1:
// callBackAsImp has type void (*)(id, SEL, id)
callBackAsImp(argument1);
break;
case 2:
// callBackAsImp has type void (*)(id, SEL, id, id)
callBackAsImp(argument1, argument2);
break;
}
When I'm constructing the callBackAsImp, I'd need to have another case
statement to type it correctly. Does this make sense? Maybe I'm
missing the point here, but I haven't figured out how to get around
this yet.
Thanks again,
Hank
Hank Heijink
email@hidden
_______________________________________________
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