OC源码分析-Runtime源码分析(四)
2019-02-01 本文已影响7人
TianTianBaby223
一 : 对象方法消息转发
当前两个阶段 都搞不定的时候调用消息转发机制.


如果forwardingTargetForSelector
返回的是nill,或者没写,则进入methodSignatureForSelector
方法,如果继续不处理返回nill,则进入doesNotRecgnizeSelector
方法,报异常,

不为nill则调用forwardinvocation
方法进行处理

二 : forwardInvocation解析
在这个方法中可以尽情处理
我们可以在这个方法中只打印

带一个age参数

获取一个参数

获取调用返回值



方法签名的另一种写法

二 : 类方法消息转发

或者

+号方法的forwardingTargetForSelector:
也可以返回实例对象,因为本质相当于 ,执行消息发送 obj_msgSend([[MJCat alloc]init],@selector(test))
,相当于调用了实例对象-号方法


三 : dynamic
提醒编译器不要自动生成setter和getter的实现、不要自动生成成员变量
动态添加set/get方法

四 : super 本质
super本质以及底层实现,super调用,底层会转换成objc_msgSendSuper2函数的调用,接受两个参数一个是 结构体参数 ,还有一个是方法名.
结构体表现

案例分析
class 与 superclass方法的底层实现
[super class]
底层调用 obj_megSendSuper.
其消息接受者是self,class在NSObject中,在其class方法中的self
是放调用者,也就是 obj_megSendSuper
的消息接受者.
[super superclass]


四 : 方法转发用法
没有实现test与other方法
