category

102 阅读1分钟

category 跟 load 两码事

runtime 运行时, 加载完 class 加载 category 加载category的时候 method 已经attached到本类方法前面,

loadAllCategories(); attachCategories(cls, &lc, 1, ATTACH_EXISTING)

然后 加载load方法,但这时load方法是直接走IMP的 : (*load_method)(cls, @selector(load));

但是在自己调用load方法的时候 走的是运行时消息转发, category load方法在本类之前。


void
load_images(const char *path __unused, const struct mach_header *mh)
{
    if (!didInitialAttachCategories && didCallDyldNotifyRegister) {
        didInitialAttachCategories = true;
        loadAllCategories();
    }

    // Return without taking locks if there are no +load methods here.
    if (!hasLoadMethods((const headerType *)mh)) return;

    recursive_mutex_locker_t lock(loadMethodLock);

    // Discover load methods
    {
        mutex_locker_t lock2(runtimeLock);
        prepare_load_methods((const headerType *)mh);
    }

    // Call +load methods (without runtimeLock - re-entrant)
    call_load_methods();
}

kingcos.me/posts/2019/…