【发布时间】:2012-11-28 09:34:50
【问题描述】:
我需要使用 NSInvocation 来动态调用一个方法。 这是我尝试过的:
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];
需要提一下,messageRecord.senderController 是要调用哪个方法的对象,messageRecord.receiverAction 是赋予这段代码的选择器。此外,message.data 是类型为 (NSArray *) 的对象,并且已正确初始化。
这段代码给出以下编译时错误
Address of property expression requested
当我如下更改调用过程时,它按预期工作:
NSArray *dataArray = message.data;
[invocation setSelector:messageRecord.receiverAction];
[invocation setTarget: messageRecord.senderController];
[invocation setArgument: &dataArray atIndex:2];
[invocation invoke];
两者之间的唯一区别是:我创建了一个本地 NSArray 指针并将 message.data 分配给它。后来,给出了新创建的指针的地址,而不是message.data 本身。
为什么有效?到底有什么区别?
【问题讨论】:
标签: objective-c pointers nsinvocation