Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use performSelector with three or more arguments?

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?

like image 335
user392412 Avatar asked Oct 05 '11 12:10

user392412


2 Answers

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

like image 75
Vladimir Avatar answered Oct 21 '22 03:10

Vladimir


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);
like image 2
BB9z Avatar answered Oct 21 '22 03:10

BB9z