Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value for performSelector:

What will the return value for performSelector: if I pass a selector that returns a primitive type (on object), such as 'week' on NSDateComponents (which will return an int)?

like image 800
cfischer Avatar asked Jun 27 '11 11:06

cfischer


People also ask

What is the use of performSelector in IOS?

The performSelector: method allows you to send messages that aren't determined until run-time. This means that you can pass a variable selector as the argument: SEL aSelector = findTheAppropriateSelectorForTheCurrentSituation(); id returnedObject = [anObject performSelector:aSelector]; But use caution when doing this.

How do you use NSInvocation?

Think of it like sending an email. You open up a new email ( NSInvocation object), fill in the address of the person (object) who you want to send it to, type in a message for the recipient (specify a selector and arguments), and then click "send" (call invoke ). See Using NSInvocation for more information.


1 Answers

An example of using NSInvocation to return a float:

SEL selector = NSSelectorFromString(@"someSelector"); if ([someInstance respondsToSelector:selector]) {     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:                                 [[someInstance class] instanceMethodSignatureForSelector:selector]];     [invocation setSelector:selector];     [invocation setTarget:someInstance];     [invocation invoke];     float returnValue;     [invocation getReturnValue:&returnValue];     NSLog(@"Returned %f", returnValue); } 
like image 56
dizy Avatar answered Oct 04 '22 04:10

dizy