消息快速转发流程
消息转发顾名思义,是将消息转发给别的接受者,也就是其他的对象
forwardingTargetForSelector
- 如果我们也没有实现
resolveInstanceMethod,查看log后可以看到之后调用了forwardingTargetForSelector - 看一下官方文档的解释
If an object implements (or inherits) this method, and returns a non-nil (and non-self) result,
that returned object is used as the new receiver object and the message dispatch resumes to that new object. (Obviously if you return self from this method, the code would just fall into an infinite loop.) If you implement this method in a non-root class, if your class has nothing to return for the given selector thenyou should return the result of invoking super’s implementation. This method gives an object a chance to redirect an unknown message sent to it before the much more expensiveforwardInvocation:machinery takes over.This is useful when you simply want to redirect messages to another object and can be an order of magnitude faster than regular forwarding. It is not useful where the goal of the forwarding is to capture the NSInvocation, or manipulate the arguments or return value during the forwarding.
- 相当于重定向
- 可以返回一个新的对象来调用该
method, - 在消耗更多的
forwardInvocation:方法之前
WLWPerson没有实现look方法,实现forwardingTargetForSelector指定WLWTeacher调用look
消息慢速转发
- 如果没有实现也
forwardingTargetForSelector就会走到methodSigntureForSeletor - 官方文档
- 通过
methodSignatrueForSelector来获得消息的签名来创建一个NSInvocation对象用来消息转发 forwardingInvocation方法实现消息转发,当然也可以不作处理- 官方demo
- 也可以调用
doesNotRecognizeSelector来实现消息不转发
源码解析
- 打印堆栈发现,消息源码来自
CFFoundation源码 - 但是产找源码发现找不对应的代码, 可能是没有开源
- 如果想要继续探索,可以通过
image list,找到对应的CoreFoundtion的可执行文件,然后再通过反汇编的方式进行探索 - 利用
Hopper工具,将找到的CoreFoundtion可执行文件拖进去