前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NSInvocation 详解

NSInvocation 详解

作者头像
星宇大前端
发布2019-01-15 15:40:46
1.8K0
发布2019-01-15 15:40:46
举报
文章被收录于专栏:大宇笔记大宇笔记

NSInvocation 简介

        在上篇文章关于消息的转发中介绍了,通过方法签名NSMethodSignature产生NSInvocation,然后配置NSInvocation参数进行消息的转发。那么NSInvocation到底是什么呢,他在OC中扮演什么角色呢?

        先感性的定义一个这个类,其实NSInvocation就是一个创建方法(消息),将方法具体化的一个类,NSInvocation能设置Target,参数,返回值和方法名称。

然后我们需要理性的看代码了如下:

代码语言:javascript
复制
@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,但是可以携带多个参数。

代码语言:javascript
复制
- (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、消息转发时候创建转发方法

代码语言:javascript
复制
/**
 现在我们有了方法的签名,现在只需要将方法打包转发
 
 @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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年07月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • NSInvocation 简介
  •         先感性的定义一个这个类,其实NSInvocation就是一个创建方法(消息),将方法具体化的一个类,NSInvocation能设置Target,参数,返回值和方法名称。
    • NSInvocation 应用
    • NSInvocation可以去构建一个方法然后转发这个方法,所以我们可以利用这两点特性去做一些事情。
    • NSInvocation的创建需要方法签名来创建,首先要得到方法签名。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档