Re: How do I get something like performSelector:(SEL) withObjects:(NSArray *)?
Re: How do I get something like performSelector:(SEL) withObjects:(NSArray *)?
- Subject: Re: How do I get something like performSelector:(SEL) withObjects:(NSArray *)?
- From: Daniel Jalkut <email@hidden>
- Date: Sat, 24 Sep 2005 19:50:26 -0400
On Sep 24, 2005, at 7:10 PM, Christoffer Lernö wrote:
Hello everyone,
I have a small problem looking for a nice solution.
I want to dynamically call different methods on a delegate.
Normally I'd use performSelector, but the method (and consequently
the arguments) cannot be restricted to 0 or 1.
The arguments are all stored in an NSArray, and the selector itself
is actually described in the same manner, being assembled before
the call is made.
Now, the way I made this work was to create an NSInvocation, having
to go by way of NSMethodDefinition etc etc. copying all the
argument pointers first into a c-style array and then moving them
to into NSInvocation, all the way I got a feeling I was doing the
whole thing wrong.
After writing some really hideous code, I got it to work.
However, there just has to be some simpler way of doing this, or?
How do I create something efficient that would be suitable as a
category to NSObject?
I was thinking it should look something along the lines of:
-(void)performSelector:(SEL)aSelector withObjects:(NSArray *)anArray
Can you explain more specifically what the requirements are? I assume
you don't want to simply have each of the delegate's callbacks parse
the contents of the objects array itself?
The underlying C function, objc_msgSend, is a varargs function, so
you can (as far as I know) pass it as many arguments as you want. I
assume it was just a case of "we have to stop somewhere" that the
standard NSObject only exposes methods for 1 and 2 arguments.
The problem is figuring out how to parse the array and suitably map
the contents of the array to a compilable, linkable, objc_msgSend
call. You might be able to use some assembly glue to set it up for
"any number of arguments," but you would probably get just as much
utility (and more portability) out of writing something gnarly that
can handle "up to as many arguments as I'm likely to use." This might
work (typed into Mail):
-(id) performSelector:(SEL)theSelector withOrderedArguments:(NSArray*)
myArgs
{
switch([myArgs count])
{
case 0: return [self performSelector:theSelector];
case 1: return [self performSelector:theSelector withObject:
[myArgs objectAtIndex:0]];
case 2: return [self performSelector:theSelector withObject:
[myArgs objectAtIndex:0] withObject:[myArgs objectAtIndex:1]];
case 3: return objc_msgSend(self, theSelector, [myArgs
objectAtIndex:0], [myArgs objectAtIndex:1], [myArgs objectAtIndex:2]);
case 4: return objc_msgSend(self, theSelector, [myArgs
objectAtIndex:0], [myArgs objectAtIndex:1], [myArgs objectAtIndex:2],
[myArgs objectAtIndex:3]);
case 5: return objc_msgSend(self, theSelector, [myArgs
objectAtIndex:0], [myArgs objectAtIndex:1], [myArgs objectAtIndex:2],
[myArgs objectAtIndex:3], [myArgs objectAtIndex:4]);
case 6: return objc_msgSend(self, theSelector, [myArgs
objectAtIndex:0], [myArgs objectAtIndex:1], [myArgs objectAtIndex:2],
[myArgs objectAtIndex:3], [myArgs objectAtIndex:4], [myArgs
objectAtIndex:5]);
default: [NSException raise:@"BooHooHoo"
format:@"performSelector:withOrderedArguments: called with too many
arguments. Add more case statements."]; break;
}
return nil;
}
Am I on the right track?
Daniel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden