iOS - runtime总结

详细总结一下 Runtime 的核心内容:

1. 消息发送机制

objectivec 复制代码
// 消息发送的基本流程
id objc_msgSend(id self, SEL _cmd, ...) {
    // 1. 获取 isa
    Class cls = object_getClass(self);
    
    // 2. 查找缓存
    IMP imp = cache_getImp(cls, _cmd);
    if (imp) return imp(self, _cmd, ...);
    
    // 3. 方法查找
    imp = lookUpImpOrForward(cls, _cmd);
    
    // 4. 执行方法
    return imp(self, _cmd, ...);
}

2. 类与对象结构

2.1 对象结构

objectivec 复制代码
struct objc_object {
    isa_t isa;  // isa 指针
};

// isa 的位域结构
union isa_t {
    uintptr_t bits;
    struct {
        uintptr_t nonpointer     : 1;  // 是否优化的 isa 指针
        uintptr_t has_assoc      : 1;  // 是否有关联对象
        uintptr_t has_cxx_dtor   : 1;  // 是否有 C++ 析构函数
        uintptr_t shiftcls       : 33; // 类的指针
        // ...其他位域
    };
};

2.2 类结构

objectivec 复制代码
struct objc_class : objc_object {
    Class superclass;
    cache_t cache;             // 方法缓存
    class_data_bits_t bits;    // 类的相关信息
    
    class_rw_t *data() {
        return bits.data();
    }
};

3. 方法缓存机制

objectivec 复制代码
struct cache_t {
    struct bucket_t *_buckets;  // 散列表
    mask_t _mask;              // 容量掩码
    mask_t _occupied;          // 已使用数量
    
    IMP imp(SEL sel) {
        bucket_t *b = buckets();
        mask_t m = mask();
        // 查找方法实现
        return findMethod(b, m, sel);
    }
};

4. 类的加载过程

objectivec 复制代码
void _objc_init(void) {
    // 1. 初始化锁
    runtime_init();
    
    // 2. 读取镜像信息
    map_images();
    
    // 3. 加载类和分类
    load_images();
    
    // 4. 调用 +load 方法
    call_load_methods();
}

void call_load_methods(void) {
    // 1. 按顺序调用类的 +load
    call_class_loads();
    
    // 2. 调用分类的 +load
    call_category_loads();
}

5. 关联对象

objectivec 复制代码
// 关联对象表
struct AssociationsManager {
    static AssociationsHashMap *_map;
    
    void setObject(id object, void *key, id value) {
        // 存储关联对象
    }
    
    id getObject(id object, void *key) {
        // 获取关联对象
    }
};

6. 弱引用机制

objectivec 复制代码
struct weak_table_t {
    weak_entry_t *weak_entries;
    size_t num_entries;
    uintptr_t mask;
    
    void insert(id referent, id *referrer) {
        // 添加弱引用
    }
    
    void remove(id referent) {
        // 移除弱引用
    }
};

7. 自动释放池

objectivec 复制代码
class AutoreleasePoolPage {
    static pthread_key_t const key = AUTORELEASE_POOL_KEY;
    id *next;
    pthread_t const thread;
    AutoreleasePoolPage *child;
    
    void *push() {
        // 压入新的自动释放池
    }
    
    void pop(void *token) {
        // 释放对象
    }
};

8. Method Swizzling

objectivec 复制代码
void method_exchangeImplementations(Method m1, Method m2) {
    if (!m1 || !m2) return;
    
    mutex_locker_t lock(runtimeLock);
    
    IMP imp1 = method_getImplementation(m1);
    IMP imp2 = method_getImplementation(m2);
    
    method_setImplementation(m1, imp2);
    method_setImplementation(m2, imp1);
}

9. 动态方法解析

objectivec 复制代码
void _class_resolveMethod(Class cls, SEL sel) {
    if (! cls->isMetaClass()) {
        // 实例方法解析
        resolveInstanceMethod(cls, sel);
    } else {
        // 类方法解析
        resolveClassMethod(cls, sel);
    }
}

10. 性能优化

objectivec 复制代码
// 1. 方法缓存
static inline IMP cache_getImp(Class cls, SEL sel) {
    cache_key_t key = cache_key(sel);
    return cls->cache.find(key);
}

// 2. isa 优化
inline Class 
objc_object::ISA() {
    assert(!isTaggedPointer()); 
    return (Class)(isa.bits & ISA_MASK);
}

关键特性:

  1. 动态性:运行时决议
  2. 消息发送机制
  3. 方法缓存优化
  4. 类动态加载
  5. 关联对象支持
  6. 弱引用管理
  7. 自动释放池
  8. 方法替换能力
  9. 动态方法解析
  10. 性能优化机制

这些特性使 Objective-C Runtime 成为一个强大而灵活的运行时系统。

相关推荐
Digitally1 小时前
如何在电脑上轻松访问 iPhone 文件
ios·电脑·iphone
安和昂1 小时前
【iOS】YYModel源码解析
ios
pop_xiaoli2 小时前
UI学习—cell的复用和自定义cell
学习·ui·ios
Daniel_Coder4 小时前
Xcode 16.4 + iOS 18 系统运行时崩溃:___cxa_current_primary_exception 符号丢失的原因与解决方案
ios·xcode·ios 18·dyld·libc++abi
烈焰晴天6 小时前
使用ReactNative加载Svga动画支持三端【Android/IOS/Harmony】
android·react native·ios
sg_knight7 小时前
Flutter嵌入式开发实战 ——从树莓派到智能家居控制面板,打造工业级交互终端
android·前端·flutter·ios·智能家居·跨平台
胖虎117 小时前
iOS上传应用包错误问题 “Invalid bundle. The “UIInterfaceOrientationPortrait”“
ios·审核·苹果审核·苹果传包
安和昂18 小时前
【iOS】ARC 与 Autorelease
macos·ios·cocoa
豪冷啊20 小时前
iOS UIActivityViewController 组头处理
ios·objective-c·xcode
二流小码农1 天前
鸿蒙开发:loading动画的几种实现方式
android·ios·harmonyos