Objective-C: How to call performSelector with a BOOL typed parameter?

Jim picture Jim · Aug 16, 2011 · Viewed 31.1k times · Source

Is there any way to send a BOOL in selector ?

[self performSelector:@selector(doSomething:) withObject:YES afterDelay:1.5];

Or I should use NSInvocation? Could somebody write a sample please ?

Answer

TomSwift picture TomSwift · Jan 4, 2013

In the case that you cannot alter the target-method signature to accept a NSNumber in place of a BOOL you can use NSInvocation instead of performSelector:

MyTargetClass* myTargetObject;
BOOL myBoolValue = YES; // or NO

NSMethodSignature* signature = [[myTargetObject class] instanceMethodSignatureForSelector: @selector( myMethodTakingBool: )];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];
[invocation setTarget: myTargetObject];
[invocation setSelector: @selector( myMethodTakingBool: ) ];
[invocation setArgument: &myBoolValue atIndex: 2];
[invocation invoke];