Re: NSInvocation question
Re: NSInvocation question
- Subject: Re: NSInvocation question
- From: Chris Suter <email@hidden>
- Date: Tue, 26 Feb 2008 10:03:24 +1100
On 26/02/2008, at 1:49 AM, Hank Heijink wrote:
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.
Why don't you just do something like I suggested in my previous e-mail:
For every callback you have, write a method on MyDocument:
For example:
- (void)doStartTrial
{
[self startTrial:trial]; // Store trial as an instance variable of
MyDocument
}
- (void)doMakeGraphic
{
[self makeGrahpic:graphic changeStatusTo:status]; // Likewise,
store graphic and status as instance variables
}
Now just pass the selector round for the callback, so that when you
want to trigger the callback, you just do:
[self performSelector:selector];
Kind regards,
Chris
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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