I need to use NSInvocation to invoke a method dynamically. Here what I have tried:
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[messageRecord.senderController class] instanceMethodSignatureForSelector:messageRecord.receiverAction]];
[invocation setSelector:messageRecord.receiverAction];
[invocation setTarget: messageRecord.senderController];
[invocation setArgument: &(message.data) atIndex:2];
[invocation invoke];
I need to mention that, messageRecord.senderController
is the object of which method will be invoked, and messageRecord.receiverAction
is the selector given to this piece of code. Besides, message.data
is an object of type (NSArray *) and initialized properly.
This piece of code gives the following compile-time error
Address of property expression requested
When I change the invocation process as following, it works as expected:
NSArray *dataArray = message.data;
[invocation setSelector:messageRecord.receiverAction];
[invocation setTarget: messageRecord.senderController];
[invocation setArgument: &dataArray atIndex:2];
[invocation invoke];
The only difference between two is that: I created a local NSArray pointer and assigned message.data to it. Later, gave address of the newly created pointer instead of message.data
itself.
Why it worked? What is the difference anyway?