美文网首页
Runtime--Method Swizzling

Runtime--Method Swizzling

作者: ffmylikes | 来源:发表于2020-02-18 20:16 被阅读0次

关于OC的特性运行时,可以实现黑魔法 Method Swizzling。

实现代码举例:

+ (void)load {

    Classclz = [selfclass];


    SELoldSEL =@selector(viewDidAppear:);

    SELnewSEL =@selector(newviewffDidAppear:);


    MethodoriginalMethod =class_getInstanceMethod(clz, oldSEL);

    MethodswizzledMethod =class_getInstanceMethod(clz, newSEL);


    BOOLdidAddMethod =class_addMethod(clz,

                                       oldSEL,

                                       method_getImplementation(swizzledMethod),

                                       method_getTypeEncoding(swizzledMethod));


    if(didAddMethod) {

        class_replaceMethod(clz,

                            oldSEL,

                            method_getImplementation(originalMethod),

                            method_getTypeEncoding(originalMethod));

    }else{

        method_exchangeImplementations(originalMethod, swizzledMethod);

   }

}


- (void) newviewffDidAppear:(BOOL)animated {

    [selfnewviewDidAppear:animated];

    WRITE_PERFORMANCE

}

相关文章

网友评论

      本文标题:Runtime--Method Swizzling

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