美文网首页
Runtime之强行调用被覆盖类中的方法

Runtime之强行调用被覆盖类中的方法

作者: wangDavid939i | 来源:发表于2019-03-13 10:43 被阅读0次
    1.关于类别覆盖原有类方法后,我们任然要想调用其原有类的方法。所用的底层转换方式
    
    #import <Foundation/Foundation.h>
    #import "CFRuntimeKit.h"
    #import "CFPerson.h"
    #import "CFPerson+Category1.h"
    #import "CFPerson+Category2.h"
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            
            CFPerson *person = [[CFPerson alloc]init];
            //方法覆盖问题验证(后面编译的分类方法在最前面,优先访问它)
            [person eating];
            //查看方法列表
            NSArray *methodArr = [CFRuntimeKit fetchMethodList:[CFPerson class]];
            NSLog(@"methodArr:%@",methodArr);
            
            /*
             *调用person类的eating方法
             */
            unsigned int outCount = 0;
            Method *methodList = class_copyMethodList([CFPerson class], &outCount);
            IMP lastImp = NULL;
            SEL lastSel = NULL;
            for (unsigned int i=0; i<outCount; i++) {
                Method method = methodList[i];
                SEL methodSEL = method_getName(method);
                NSString *methodName =[NSString stringWithUTF8String:sel_getName(methodSEL)];
                if ([methodName isEqualToString:@"eating"]) {
                    lastImp = method_getImplementation(method);
                    lastSel = method_getName(method);
                }
            }
            ((void (*)(id,SEL))(void *)lastImp)(person,lastSel);
            free(methodList);
        }
        
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:Runtime之强行调用被覆盖类中的方法

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