Method Swizzling 为什么要先调用 class_addMethod?

class_addMethod will add an override of a superclass's implementation, 

but will not replace an existing implementation in this class. 

To change an existing implementation, use method_setImplementation.

 

给指定Class添加一个SEL的实现(或者说是SEL和指定IMP的绑定),添加成功返回YES,SEL已经存在或添加失败返回NO。
它有两个需要注意的点:

如果该SEL在父类中有实现,则会添加一个覆盖父类的方法;
如果该Class中已经有SEL,则返回NO。

执行class_addMethod能避免干扰到父类,这也是为什么推荐大家尽量先使用class_addMethod的原因

 

https://juejin.cn/post/7072297877879390238

 

https://www.jianshu.com/p/bf40593fc9ed

posted @ 2022-02-17 19:52  zzfx  阅读(54)  评论(0编辑  收藏  举报