美文网首页
OC_Runtime小笔记

OC_Runtime小笔记

作者: QihuaZhou | 来源:发表于2016-07-04 16:59 被阅读30次

    最近开始在上海实习,在公司负责研究JSPatch热修复的相关事务,其中需要好好学习一下Runtime,这一块是OC里面比较晦涩的东西,现将学习心得记录下来。

    边写边学

    有两个类:ViewControllerNewViewController,分别有一个打印方法:testnewTest

    - (void)test
    {
        NSString *cls = NSStringFromClass([self class]);
        NSString *sle = NSStringFromSelector(_cmd);
        NSLog(@"origin methods: <%@> [%@]", cls, sle);
    }
    
    - (void)newTest
    {
        NSString *cls = NSStringFromClass([self class]);
        NSString *sle = NSStringFromSelector(_cmd);
        NSLog(@"now replaced method: <%@> [%@]", cls, sle);
    }
    

    下面可以在AppDelegate中的application:didFinishLaunchingWithOptions:执行以下代码,查看输出结果,理解Runtime中动态消息转发的细节。
    代码各部分的执行说明可以见注释。

        // 原本要执行的实例中的方法
        Class oriCls = NSClassFromString(@"ViewController");
        id viewController = [[oriCls alloc] init];
        SEL oriSel = NSSelectorFromString(@"test");
        [viewController performSelector:oriSel];
        
        Method oriMethod = class_getInstanceMethod(oriCls, oriSel);
        IMP originIMP = method_getImplementation(oriMethod);
        const char *oriTypes = method_getTypeEncoding(oriMethod);
        /*
        1、performSelector是运行时系统负责去找方法的,在编译时候不做任何校验;如果直接调用编译是会自动校验。如果imageDownloader:didFinishWithImage:image:不存在,那么直接调用 在编译时候就能够发现(借助Xcode可以写完就发现),但是使用performSelector的话一定是在运行时候才能发现(此时程序崩溃);Cocoa支持在运行时向某个类添加方法,即方法编译时不存在,但是运行时候存在,这时候必然需要使用performSelector去调用。所以有时候如果使用了performSelector,为了程序的健壮性,会使用检查方法
        - (BOOL)respondsToSelector:(SEL)aSelector;
        2、直接调用方法时候,一定要在头文件中声明该方法的使用,也要将头文件import进来。而使用performSelector时候, 可以不用import头文件包含方法的对象,直接用performSelector调用即可。
         */
        
        
        
        // 现在替换IMP指针到NewViewController中的newTest中
        Class newCls = NSClassFromString(@"NewViewController");
        SEL newSle = NSSelectorFromString(@"newTest");
        Method newMethod = class_getInstanceMethod(newCls, newSle);
        
        //IMP replaceIMP = class_getMethodImplementation(newCls, newSle);
        IMP replaceIMP = method_getImplementation(newMethod);
        const char *newTypes = method_getTypeEncoding(newMethod);
        
        // 然后继续执行原方法,原方法的Class和SEL仍旧是ViewController和test,但是动态执行时被替换成了NewViewController和newTest
        class_replaceMethod(oriCls, oriSel, replaceIMP, newTypes);
        // 新增一个oriTest方法,指向原来的test实现
        class_addMethod(oriCls,@selector(oriTest), originIMP, oriTypes);
        
        
        [viewController performSelector:oriSel];
        [viewController performSelector:@selector(oriTest)];
    
    

    执行结果如下图:

    执行结果

    从结果中可以清楚地看到方法的替换和保存。

    完善内容,待更~

    如果这篇文章对您有帮助,欢迎点赞和转发。有任何问题或者建议,也欢迎留言!

    相关文章

      网友评论

          本文标题:OC_Runtime小笔记

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