15

The various performSelector:... methods can handle a maximum of two arguments passed to the specified selector. What can I do if I need to pass three or more arguments?

Keavon
  • 6,837
  • 9
  • 51
  • 79
user392412
  • 743
  • 12
  • 18
  • 1
    Yeah, if you have control over the callee side the simplest approach is to throw the parms into an NSDictionary. And even if you don't "own" the callee you can often make a "glue" routine in your own class to call it (assuming you're creating the selector, vs having it handed to you). Otherwise the NSInvocation approach is what you need. – Hot Licks Oct 05 '11 at 12:19
  • possible duplicate of [performSelector with more than 2 objects](http://stackoverflow.com/questions/2346733/performselector-with-more-than-2-objects) – jscs Oct 05 '11 at 18:44

2 Answers2

13

You need to use NSInvocation class for that. Check this SO question for more details on using them.

Community
  • 1
  • 1
Vladimir
  • 170,431
  • 36
  • 387
  • 313
3

I dislike the NSInvocation way, it needs too much code.

If you’d like perform the selector immediately, here is an simple and clean way:

// Assume we have these variables
id target, SEL aSelector, id parameter1, id parameter2;

// Get the method IMP, method is a function pointer here.
id (*method)(id, SEL, id, id) = (void *)[vc methodForSelector:aSelector];

// IMP is just a C function, so we can call it directly.
id returnValue = method(vc, aSelector, parameter1, parameter2);
Alexander
  • 59,041
  • 12
  • 98
  • 151
BB9z
  • 2,432
  • 1
  • 30
  • 36