美文网首页runtime 相关的
用runtime实现类方法交换

用runtime实现类方法交换

作者: 5a9c6f44e578 | 来源:发表于2017-08-19 10:06 被阅读7次

面试有被问道runtime,概念性的就不讲了,具体来实现一个功能,交换类方法

先创建两个model,记得在.h中放出接口

image.png image.png

实现时,先 #import <objc/runtime.h>

        Method method1 = class_getInstanceMethod([object class], @selector(methodOne));
        Method method2 = class_getInstanceMethod([TWO class], @selector(methodTwo));
        method_exchangeImplementations(method1, method2);
        
        object *o = [object new];
        [o methodOne];

打印结果

image.png

method_exchangeImplementations 交换IMP指针
利用runtime的特性,动态的交换IMP 指针.从而实现交换类方法

相关文章

  • RunTime实现

    1:RunTiem交换方法实现 //runTime交换方法实现 // 1,创建已有类的分类,并且实现自己的方...

  • 用runtime实现类方法交换

    面试有被问道runtime,概念性的就不讲了,具体来实现一个功能,交换类方法 先创建两个model,记得在.h中放...

  • Runtime

    runtime运行时机制1:通过runtime,实现方法交换(交换两个类方法、交换两个实例方法)2:通过runti...

  • Day3

    1 runtime运行时机制1:通过runtime,实现方法交换(交换两个类方法、交换两个实例方法)。2:通过ru...

  • ios-Runtime(运行时)

    利用runtime来实现归档解档 方法交换 俗称 OC的方法欺骗 KVO的实现原理 用runtime来实现KVO...

  • iOS - RunTime(Swift)

    RunTime实现存储属性(本质也是一个计算属性) RunTime实现方法交换

  • iOS method swizzling 面向切面无痕打点

    重载类的“+(void)load”方法,在程序加载到内存时利用Runtime的 等接口将方法的实现互相交换。当方法...

  • iOS 开发之runtime使用小结

    我们一般用runtime做以下这些事情: 一、使用runtime如何交换两个方法的实现,拦截系统自带的方法调用功能...

  • 转:Runtime的使用

    一、交换两个方法的实现,拦截系统自带的方法调用功能 需要用到的方法 获得某个类的类...

  • iOS runtime如何交换两个类方法

    如有转载,请标明出处:iOS runtime如何交换两个类方法 runtime交换实例方法,老生常谈的问题,很多b...

网友评论

    本文标题:用runtime实现类方法交换

    本文链接:https://www.haomeiwen.com/subject/gittdxtx.html