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)?
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.
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.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With