b2cloud

30th January 2015

iOS performSelector with multiple parameters

Guides | Tutorial By 2 years ago

On iOS the built in convenience method performSelector method call only allows for up to 2 parameters.

- (void) aMethod
{
	[self performSelector:@selector(doSomethingWithObject:otherObject:) withObject:@(1) withObject:@(2)];
}

- (void) doSomethingWithObject:(id) object otherObject:(id) otherObject
{
	//	Code here
}

You may find a situation where you want to use more than 2 parameters.

This can be done like so:

- (void) anotherMethod
{
	id target = self;
	SEL selector = @selector(doSomethingElseWithObject:otherObject:yetAnotherObject:);
	
	//	First first and second arguments are for the target and selector,
	//	the remaining argument types match those of the selector
	typedef void (*MethodType)(id, SEL, id, id, id);
	MethodType methodToCall = (MethodType)[target methodForSelector:selector];
	methodToCall(target, selector, @(1), @(2), @(3));
}

- (void) doSomethingElseWithObject:(id) object otherObject:(id) otherObject yetAnotherObject:(id) yetAnotherObject
{
	//	Code here
}
Recommended Posts

Yammer integrations in ReactJS

Post by 2 years ago

I am writing this blog while I am working on a project for our client’s intranet website. The client requires the website has the ability to share, like and write comments in the website through

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!