美文网首页
runtime 交换实例方法和对象方法标准写法

runtime 交换实例方法和对象方法标准写法

作者: kelvin943 | 来源:发表于2017-02-07 17:08 被阅读58次

交换实例方法:其中Class参数一定得是实例对象的类(实例对象的isa指向它的Class(储存所有减号方法)object_getClass(obj))

static void _exchanged_instance_method(SEL originalSelector, SEL swizzledSelector, Class class){
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    BOOL didAddMethod =
    class_addMethod(class,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));
    
    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    }
    else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

交换静态方法(类): 其中class 参数一定要元类(metaClass),Class对象的isa指向元类(储存所有加号方法)object_getClass(object_getClass(obj))

static void _exchanged_class_method(SEL originalSelector, SEL swizzledSelector, Class class){
    Method originalMethod = class_getClassMethod(class, originalSelector);
    Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
    BOOL didAddMethod =
    class_addMethod(class,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));
    
    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    }
    else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

注意: 静态方法中的self 是类对象,实例方法中的self 是实例对象,可以用一下方法测试一下:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self  instanceMethod];
    [ViewController classMethod];
}

- (void)instanceMethod {
    NSLog(@"%p",self);
    NSLog(@"%p",object_getClass((id)self));
}

+ (void)classMethod1 {
    NSLog(@"%p",self);
   //[self instanceMethod]       会报错找不到instanceMethod 方法
   //[self classMethod2]         编译器不会报错而且能正常调用
}
+ (void)classMethod2{
    NSLog(@"%p",self);

}

最后附一幅经典的图:

相关文章

  • runtime 交换实例方法和对象方法标准写法

    交换实例方法:其中Class参数一定得是实例对象的类(实例对象的isa指向它的Class(储存所有减号方法)obj...

  • Runtime

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

  • runTime常用方法

    使用runTime改变实例成员的值 使用runtime来交换两个方法 注意再次调用该方法不交换 使用runTime...

  • Day3

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

  • Objective-C Runtime: 变量和属性

    本文只是整理Runtime中,成员变量、属性、关联对象、方法交换使用实例。不会很细致的讲解Runtime的内容,如...

  • 参考YYImage,让页面自动支持gif、webp等格式图片

    参考: iOS Runtime交换对象方法和类方法探讨 YYImage SDWebImage ----软件--- ...

  • SDWebImage、YYWebImage让UIImageVie

    参考: iOS Runtime交换对象方法和类方法探讨 YYImage SDWebImage SDWebimage...

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

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

  • Category实现原理

    依赖runtime 动态的将分类的方法和类方法合并到类对象和元类对象的方法列表中 (对实例对象 类对象 元类对...

  • JavaScript中原型的简单语法

    简单的原型写法 原型中的方法是可以互相访问的 实例对象使用的属性和方法层层的搜索 实例对象使用的属性或者方法,先在...

网友评论

      本文标题:runtime 交换实例方法和对象方法标准写法

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