NSInvocation 详解
来源:互联网 发布:多益网络公司地址 编辑:程序博客网 时间:2024/05/17 08:31
NSInvocation 简介
在上篇文章关于消息的转发中介绍了,通过方法签名NSMethodSignature产生NSInvocation,然后配置NSInvocation参数进行消息的转发。那么NSInvocation到底是什么呢,他在OC中扮演什么角色呢?先感性的定义一个这个类,其实NSInvocation就是一个创建方法(消息),将方法具体化的一个类,NSInvocation能设置Target,参数,返回值和方法名称。
然后我们需要理性的看代码了如下:
@interface NSInvocation : NSObject {@private __strong void *_frame; __strong void *_retdata; id _signature; id _container; uint8_t _retainedArgs; uint8_t _reserved[15];}// 通过NSMethodSignature对象创建NSInvocation对象,NSMethodSignature为方法签名类+ (NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)sig;// 获取NSMethodSignature对象@property (readonly, retain) NSMethodSignature *methodSignature;// 保留参数,它会将传入的所有参数以及target都retain一遍- (void)retainArguments;// 判断参数是否还存在// 调用retainArguments之前,值为NO,调用之后值为YES@property (readonly) BOOL argumentsRetained;// 设置消息调用者,注意:target最好不要是局部变量@property (nullable, assign) id target;// 设置要调用的消息@property SEL selector;// 获取消息返回值- (void)getReturnValue:(void *)retLoc;// 设置消息返回值- (void)setReturnValue:(void *)retLoc;// 获取消息参数- (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx;// 设置消息参数- (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx;// 发送消息,即执行方法- (void)invoke;// target发送消息,即target执行方法- (void)invokeWithTarget:(id)target;@end
NSInvocation 应用
NSInvocation可以去构建一个方法然后转发这个方法,所以我们可以利用这两点特性去做一些事情。
NSInvocation的创建需要方法签名来创建,首先要得到方法签名。
NSMethodSignature类就不介绍了,一看便知。
1、类似performSelector,但是可以携带多个参数。
- (id)performSelector:(SEL)aSelector withArguments:(NSArray *)arguments { if (aSelector == nil) return nil; NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:aSelector]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; invocation.target = self; invocation.selector = aSelector; // invocation 有2个隐藏参数,所以 argument 从2开始 if ([arguments isKindOfClass:[NSArray class]]) { NSInteger count = MIN(arguments.count, signature.numberOfArguments - 2); for (int i = 0; i < count; i++) { const char *type = [signature getArgumentTypeAtIndex:2 + i]; // 需要做参数类型判断然后解析成对应类型,这里默认所有参数均为OC对象 if (strcmp(type, "@") == 0) { id argument = arguments[i]; [invocation setArgument:&argument atIndex:2 + i]; } } } [invocation invoke]; id returnVal; if (strcmp(signature.methodReturnType, "@") == 0) { [invocation getReturnValue:&returnVal]; } // 需要做返回类型判断。比如返回值为常量需要包装成对象,这里仅以最简单的`@`为例 return returnVal;}作者:01_Jack链接:http://www.jianshu.com/p/177e44a411db
2、消息转发时候创建转发方法
/** 现在我们有了方法的签名,现在只需要将方法打包转发 @param anInvocation 通过签名得到的方法转发对象 */-(void)forwardInvocation:(NSInvocation *)anInvocation{ if ([NSStringFromSelector(anInvocation.selector) isEqualToString:@"R"]) { [anInvocation setTarget:self]; [anInvocation setSelector:@selector(R:)]; NSString * name = @"流浪法师"; //第一个和第二个参数是target和sel [anInvocation setArgument:&name atIndex:2]; [anInvocation retainArguments]; [anInvocation invoke]; NSString * returnStr = [NSString stringWithFormat:@"%@死于大招之下",name]; [anInvocation setReturnValue:&returnStr]; [anInvocation getReturnValue:&returnStr]; } }-(NSString *)R:(NSString *)emery{ return nil;}
阅读全文
0 0
- NSInvocation 详解
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- An NSInvocation
- NSInvocation用法
- NSInvocation使用
- NSInvocation 学习
- iOS NSInvocation
- NSInvocation 注意事项
- NSInvocation使用
- NSInvocation crash
- NSInvocation简单使用
- 安装APK文件
- RocketMQ原理解析-consumer 6.pull消息消费
- 算法---比较排序算法
- JavaScript翻转字符串,把hello world. 变成 dlrow. olleh
- POJ-1724 ROADS
- NSInvocation 详解
- 一些需要解决的东西
- LinuxC学习日常——自习(1)
- linux 清除左边一列的”
- 创建不会被系统扫描文件夹
- Linux防火墙iptables(一)入门介绍(扫盲)
- codevs1132 瑞士轮 归并排序
- MMU内存管理单元(2)-访问权限控制
- MyEclipse中文注释乱码问题