Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 886
Jim Avatar asked Aug 16 '11 08:08

Jim


2 Answers

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]; 
like image 81
TomSwift Avatar answered Oct 15 '22 02:10

TomSwift


you can use NSNumber to wrap bools types:

BOOL myBool = YES; NSNumber *passedValue = [NSNumber numberWithBool:myBool]; [self performSelector:@selector(doSomething:) withObject:passedValue afterDelay:1.5]; 

and in the selector, to get the bool value, you use:

BOOL value = [recievedObject boolValue]; 
like image 37
Niv Avatar answered Oct 15 '22 01:10

Niv