美文网首页
iOS指令Clang查看runtime底层实现源码,报错./Vi

iOS指令Clang查看runtime底层实现源码,报错./Vi

作者: 东方诗空 | 来源:发表于2020-08-05 11:48 被阅读0次

    在iOS学习和使用中,有时想看一下对应类或方法的底层实现,来加深我们的理解,那么我们就要用到Clang指令来查看。

    1、制定目标文件路径

    cd /Users/weidong/Desktop/study/clangLoadview/clangLoadview
    执行:clang -rewrite-objc ViewController.m

    随后报错:
    ./ViewController.h:9:9: fatal error: 'UIKit/UIKit.h' file not found


    image.png

    这是在反编译时找不到对应的解析文件

    解决方式
    1、使用指定路径命令

    clang -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk /Users/weidong/Desktop/study/clangLoadview/clangLoadview/ViewController.m
    

    2、修改全局变量

    vim ~/.bash_profile
    在终端输入i键入

    alias rewriteoc='clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk'
    

    点击esc返回输入:wq退出

    终端再次输入 source ~/.bash_profile 环境变量生效

    再次编译成C++代码
    只需要输入 rewriteoc + 文件的绝对路径

    在工程文件下,可以找到对应的.cpp类,打开就可以看到源码实现了。


    image.png

    查看源码:
    摘抄部分源码实现,可以在里面搜索你想看的

    #pragma clang assume_nonnull end
    
    
    #ifndef _REWRITER_typedef_ViewController
    #define _REWRITER_typedef_ViewController
    typedef struct objc_object ViewController;
    typedef struct {} _objc_exc_ViewController;
    #endif
    
    struct ViewController_IMPL {
        struct UIViewController_IMPL UIViewController_IVARS;
    };
    
    
    
    /* @end */
    
    
    
    // @interface ViewController ()
    
    /* @end */
    
    
    // @implementation ViewController
    
    static void _I_ViewController_loadView(ViewController * self, SEL _cmd) {
        ((void (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("ViewController"))}, sel_registerName("loadView"));
    
    }
    
    
    static void _I_ViewController_viewDidLoad(ViewController * self, SEL _cmd) {
        ((void (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("ViewController"))}, sel_registerName("viewDidLoad"));
    
    }
    
    
    // @end
    
    struct _prop_t {
        const char *name;
        const char *attributes;
    };
    
    struct _protocol_t;
    
    struct _objc_method {
        struct objc_selector * _cmd;
        const char *method_type;
        void  *_imp;
    };
    

    相关文章

      网友评论

          本文标题:iOS指令Clang查看runtime底层实现源码,报错./Vi

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