NSInvocation class的介绍

2016-10-23  本文已影响70人  Bel李玉

NSInvocation对象用来在对象和应用之间存储和传递消息。其中包含target,selector,arguments(参数)和return值,当分发一个NSInvocation对象的时候,这些值都会自动分配。NSInvocation可以被多次分发,当分发给不同的对象的时候,他的参数可以随之改变,NSInvocation 的灵活性,使它在不同对象传递时变得非常有用,你可以使用invocationWithMethodSignature: 来创造一个NSInvocation对象,但不能用alloc和init方法来创建NSInvocation对象

NSInvocation对象,遵守NSCoding协议,但不能用NSPortCoder编码,也不能用NSArchive归档

首先,创造一个NSInvocation对象

+ (NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)sig;

配置NSInvocation对象

selector:接收者的方法选择器。

target: 目标,也可以没有

        - setArgument:atIndex:设置参数

        - getArgument:atIndex:获取参数

         - setReturnValue:设置返回值

         - getReturnValue:获取返回值

分发Invocation对象

-invoke:向接收对象发送消息,在发送消息之前需要设置接受者的target,selector

-- invokeWithTarget:同上

上一篇 下一篇

猜你喜欢

热点阅读