结论1、签名的参量类型伪造不正确会导致崩溃。

结论二、签名个数不对可能会导致参量丢失。

结论三:在签名配置正确的情况下,系统会将函数调用的所有信息打包到NSInvocation准备转发;

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[self performSelector:@selector(goooo:) withObject:@"dddd" afterDelay:0];

[self performSelector:@selector(exgoo) withObject:@"aa" withObject:@"bb"];

}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

{

//NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:"v@:@"];

NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:"v@:f"];

return signature;

}

- (void)forwardInvocation:(NSInvocation *)anInvocation

{

__autoreleasing id returnObj;

[anInvocation getArgument:&returnObj atIndex:(NSInteger)2];

//dddd

//aa

NSLog(NSStringFromSelector(anInvocation.selector));

//exgoo::

//goooo:

NSLog(returnObj);

}

NSInvocation的调用有2个方法,target参数可以直接设置target属性或者设置为第一个参数,如果不设置则调用第二个方法将target传入

// 测试
// 构建对象 测试UILabel的setText:方法
UILabel *myObj = [UILabel new];
NSLog(@"invocation执行前myObj.text=%@", myObj.text);
// 构建方法签名返回类型void编码为v,对象UILabel类型编码为@,SEL编码为:,参数类型NSString编码为@
NSMethodSignature *myMethodSignature = [NSMethodSignature signatureWithObjCTypes:"v@:@"];
// 构建NSInvocation
NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:myMethodSignature];
// 设置第1个参数
myInvocation.target = myObj;
// 设置第2个参数
myInvocation.selector = @selector(setText:);
// 设置第3个参数
NSString *newText = @"change new text";
[myInvocation setArgument:&newText atIndex:2];
[myInvocation retainArguments];
// 执行
[myInvocation invoke];
NSLog(@"invocation执行后myObj.text=%@", myObj.text);

作者:不上火喝纯净水
链接:https://www.jianshu.com/p/49151a79ac6a
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

oc消息转发:forwardInvocation、签名、参量个数、SEL 相关测试的更多相关文章

  1. iOS runtime探究(二): 从runtime開始深入理解OC消息转发机制

    你要知道的runtime都在这里 转载请注明出处 http://blog.csdn.net/u014205968/article/details/67639289 本文主要解说runtime相关知识, ...

  2. oc消息转发机制本质

    第一级转发:主体不变,动态添加对应函数: 第二级转发:函数不变,切换接收主体: 第三极转发:函数签名不变,主体和函数都有可能变化: 1.使用函数的动态添加技术: 2.使用的类似is-a swzzing ...

  3. runtime之消息转发

    前言 在上一篇文章中我们初尝了runtime的黑魔法,可以在程序编译阶段就获取到成员变量的名字,特性以及动态的给对象增加属性等等,在接下来中我们进一步了解OC的消息发送机制.如果之前没接触过runti ...

  4. OC:浅析Runtime中消息转发机制

    一.介绍 OC是一门动态性语言,其实现的本质是利用runtime机制.在runtime中,对象调用方法,其实就是给对象发送一个消息,也即objc_msgSend().在这个消息发送的过程中,系统会进行 ...

  5. 使用methodSignatureForSelector与forwardInvocation实现消息转发 (转)

    转自:http://blog.sina.com.cn/s/blog_8c87ba3b0102v006.html 在给程序添加消息转发功能以前,必须覆盖两个方法,即methodSignatureForS ...

  6. iOS开发·runtime原理与实践: 消息转发篇(Message Forwarding) (消息机制,方法未实现+API不兼容奔溃,模拟多继承)...

    本文Demo传送门: MessageForwardingDemo 摘要:编程,只了解原理不行,必须实战才能知道应用场景.本系列尝试阐述runtime相关理论的同时介绍一些实战场景,而本文则是本系列的消 ...

  7. Runtime-消息发送和消息转发

    消息发送 消息发送举例:下面这个OC代码 [person read:book]; 会被编译成: objc_msgSend(person, @selector(read:), book); objc_m ...

  8. iOS开发-消息转发

    消息转发是OC运行时比较重要的特性,Objective-C运行时的主要的任务是负责消息分发,我们在开发中"unrecognized selector sent to instance xx& ...

  9. Runtime 运行时之一:消息转发

    解释一 上一篇文章咱们提到了Runtime的消息传递机制,主要围绕三个C语言API来展开进行的.这篇文章我将从另外三个方法来描述Runtime中另一个特性:消息转发机制. 一.消息转发机制 当向某个对 ...

随机推荐

  1. spring定时任务(@Scheduled注解)

    (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.org/schema/task" http://www.spr ...

  2. CSS3文本属性

    text-shadow(文本阴影) <h1>我有文字阴影</h1> h1{ color:#ffff00; text-shadow:1px 2px 2px #000000; } ...

  3. Spring boot应用踩坑集锦

    Spring boot应用踩坑集锦 spring boot是spring的一种开发套件,是spring cloud的基础框架,要学习spring cloud微服务是绕不开的,遇到一些踩坑问题在这里记录 ...

  4. [android] 手机卫士黑名单功能(ListView优化)

    上一篇记录了使用ListView展示出来了100条数据,当慢慢拖动的时候,不会有问题,但是当拖动很快的时候,应用会报anr错误 查看错误日志,看到报OutOfMemoryError,内存不足 List ...

  5. Spring系列之——springboot解析resources.application.properties文件

    摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfiguration ...

  6. mac 下mongodb connect failed 连接错误

    我是用brew install mongod 安装的 MongoDB shell version v3.4.2connecting to: mongodb://127.0.0.1:270172017- ...

  7. xamarin.android App在后台运行不退出

    /// <summary> /// 重写按键事件 /// </summary> /// <param name="keyCode"></p ...

  8. sql SUM求和

  9. vue如何实现代码打包分离(按需加载)

    在vue中使用import()来代替require.ensure()实现代码打包分离 一.require.ensure() 方法来实现代码打包分离 require.ensure() 是 webpack ...

  10. CSS字体无法设置成功的问题

    在 CSS 中设置字体名称,直接写中文是可以的.但是在文件编码(GB2312.UTF-8 等)不匹配时会产生乱码的错误.xp 系统不支持 类似微软雅黑的中文. 方案一: 你可以使用英文来替代. 比如 ...