OC底层探索(十四)_objc_init分析——load_images分析

OC底层文章汇总

load_images分析

  • 在objc源码中,查看load_images源码,查看 prepare_load_methodscall_load_methods方法
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();
}

👇

查找load方法并加载

  • 查看prepare_load_methods源码,分别获取到非懒加载类的列表和非懒加载分类的列表,并循环加载load方法。查看schedule_class_load方法。
void prepare_load_methods(const headerType *mhdr)
{
   
    size_t count, i;

    runtimeLock.assertLocked();

//非懒加载类的列表
    classref_t const *classlist = 
        _getObjc2NonlazyClassList(mhdr, &count); 
        //循环加载非懒加载类的load方法
    for (i = 0; i < count; i++) {
   
        schedule_class_load(remapClass(classlist[i]));
    }

//获取非懒加载分类
    category_t * const *categorylist = _getObjc2NonlazyCategoryList(mhdr, &count);
    for (i = 0; i < count; i++) {
   
        category_t *cat = categorylist[i];
        Class cls = remapClass(cat->cls);
        if (!cls) continue;  // category for ignored weak-linked class
        if (cls->isSwiftStable()) {
   
            _objc_fatal("Swift class extensions and categories on Swift "
                        "classes are not allowed to have +load methods");
        }
        realizeClassWithoutSwift(cls, nil);
        ASSERT(cls->ISA()->isRealized());
        add_category_to_loadable_list(cat)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值