美文网首页
Objc_msgSend工作原理

Objc_msgSend工作原理

作者: Redman | 来源:发表于2016-11-06 10:34 被阅读0次

    在 Objective-C 中消息直到运行时才会绑定到方法的实现上面. 编译器会吧 [obj doSomething] 转换成 objc_msgSend(obj, @selector(doSomething));

    检查selector是否需要忽略。(ps: Mac开发中开启GC就会忽略retain,release方法。)
    
    检查target是否为nil。如果为nil,直接cleanup,然后return。(这就是我们可以向nil发送消息的原因。)
    
    然后在target的Class中根据Selector去找IMP
    

    寻找IMP的过程:

    先从当前class的cache方法列表(cache methodLists)里去找
    
    找到了,跳到对应函数实现
    
    没找到,就从class的方法列表(methodLists)里找
    
    还找不到,就到super class的方法列表里找,直到找到基类(NSObject)为止
    
    最后再找不到,就会进入动态方法解析和消息转发的机制。(这部分知识,下次再细谈)
    

    objc_class
    struct objc_class {
    Class isa OBJC_ISA_AVAILABILITY;
    #if !OBJC2
    Class super_class OBJC2_UNAVAILABLE;
    const char *name OBJC2_UNAVAILABLE;
    long version OBJC2_UNAVAILABLE;
    long info OBJC2_UNAVAILABLE;
    long instance_size OBJC2_UNAVAILABLE;
    struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
    struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
    struct objc_cache *cache OBJC2_UNAVAILABLE;
    struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
    #endif
    } OBJC2_UNAVAILABLE;

    struct objc_method_list {
    struct objc_method_list *obsolete OBJC2_UNAVAILABLE;
    int method_count OBJC2_UNAVAILABLE;

    ifdef LP64

    int space                                                OBJC2_UNAVAILABLE;
    

    endif

    /* variable length structure */
    struct objc_method method_list[1]                        OBJC2_UNAVAILABLE;
    

    }

    相关文章

      网友评论

          本文标题:Objc_msgSend工作原理

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