NSInvocation (sending arguments with @selector)

Discussion in 'iOS Programming' started by Darkroom, Aug 9, 2009.

  1. Darkroom Guest


    Dec 15, 2006
    Montréal, Canada
    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];
    any thoughts?
  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    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.
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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.
  4. kainjow Moderator emeritus


    Jun 15, 2000
    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.

Share This Page