Object-c Runtime 从代码到代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Object-c Runtime 从代码到代码相关的知识,希望对你有一定的参考价值。

关于Object C 中object, Class, Meta Class 的关系,在objc4的源代码中有如下的调用:

LibObjc 动态库被调用后,下面的函数会依次调用

asm

_map_images:

...

jmp _map_2_images

map_2_images ->

map_images_nolock ->

_read_images ->

_objc_connect_classes_from_image ->

connect_class ->

really_connect_class ->

set_superclass 

 

/***********************************************************************

* objc_allocateClass.

**********************************************************************/

void set_superclass(Class cls, Class supercls, bool cls_is_new)

{

    Class meta = cls->ISA();

 

    if (supercls) {

        cls->superclass = supercls;

        meta->superclass = supercls->ISA();

        meta->initIsa(supercls->ISA()->ISA());

 

        // Propagate C++ cdtors from superclass.

        if (supercls->info & CLS_HAS_CXX_STRUCTORS) {

            if (cls_is_new) cls->info |= CLS_HAS_CXX_STRUCTORS;

            else cls->setInfo(CLS_HAS_CXX_STRUCTORS);

        }

 

        // Superclass is no longer a leaf for cache flushing

        if (supercls->info & CLS_LEAF) {

            supercls->clearInfo(CLS_LEAF);

            supercls->ISA()->clearInfo(CLS_LEAF);

        }

    } else {

        cls->superclass = Nil// superclass of root class is nil

        meta->superclass = cls; // superclass of root metaclass is root class

        meta->initIsa(meta);    // metaclass of root metaclass is root metaclass

 

        // Root class is never a leaf for cache flushing, because the 

        // root metaclass is a subclass. (This could be optimized, but 

        // is too uncommon to bother.)

        cls->clearInfo(CLS_LEAF);

        meta->clearInfo(CLS_LEAF);

    }    

}

 

set_superclass 中的注释明确的说明了Class, meta Class,  Class Isa,  meta Isa, Class Super Class, meta super Class 之间的关系和赋值。

以上是关于Object-c Runtime 从代码到代码的主要内容,如果未能解决你的问题,请参考以下文章

iOS (Object-C) 非单元测试状态下代码覆盖率获取尝鲜

Object-C iOS纯代码布局 一堆代码可以放这里!

object-c的代码例子

代码分享-Swift版的和Object-C版倒计时

Object-C 中的 Swift 代码:“ClassName”没有可见的@interface 声明选择器“alloc”

Object-C基础—— 函数