PDA

View Full Version : NSInvocation (sending arguments with @selector)




Darkroom
Aug 9, 2009, 08:57 PM
recently i because quite disappointed when i learned that it's not possible to actually pass arguments with @selector() methods... i don't really know why that is, and it kinda pisses me off, but whatev...

so i'm trying to use NSInvocation to pass arguments thru a switch statement, but i need a little help. the following code isn't working:


NSUInteger tapCount = [[touches anyObject] tapCount];
NSInvocation *invoke = [[NSInvocation alloc] init];
switch (tapCount)
{
case (2): {
[invoke setSelector:@selector(moveImage:withTouchPoint:withTapCount:)];
[invoke setArgument:nil atIndex:0];
[invoke setArgument:nil atIndex:1];
[invoke setArgument:tapCount atIndex:2];
[self performSelector:invoke withObject:nil afterDelay:kTapDelay];
}
//etc.



any thoughts?



robbieduncan
Aug 10, 2009, 04:06 AM
You have set the first two arguments to nil. Does the method support that? Or does it assume that that they are valid non-nil values?

To let us help you: what, exactly, is not working? Describe in detail what is happening and what you expect to happen.

PhoneyDeveloper
Aug 10, 2009, 01:00 PM
You don't need an invocation to use performSelector. I don't think your code will do what you expect.

Use a dictionary. Add your parameters to it and set the the withObject parameter to the dictionary.

kainjow
Aug 10, 2009, 01:06 PM
You shouldn't set arguments 0 and 1. Just start your arguments at index 2. See the docs for the setArgument:atIndex: method for why this is. Even if you fix this, your usage of NSInvocation is incorrect.