Runtime相关

栏目: IOS · 发布时间: 5年前

  • objc_object

    Runtime相关
  • objc_class

    Runtime相关
    • cache_t

      • 用于快速查找方法执行函数
      • 是可增量扩展的哈希结构
      • 是局部性原理的最佳应用
      Runtime相关
    • class_data_bits_t

      • class_data_bits_t主要是对class_rw_t的封装
      • class_rw_t代表类相关的读写信息、对class_ro_t的封装
      • class_ro_t代表类相关的只读信息
        Runtime相关
  • isa 指针

    Runtime相关
    • 实例对象,isa指向其类对象

    • 类对象,isa指向其元类对象

  • method_t

    Runtime相关
    • Type Encodings

      • const char* types;
        Runtime相关
  • 总体

    Runtime相关

2. 对象、类对象、元类对象

  • 类对象存储实例方法列表等信息
  • 元类对象存储类方法列表等信息
    Runtime相关

3. 消息传递

  • super

    void objc_msgSend(void /* id self, SEL op, ... */)
    
    // [self class] => objc_msgSend(self, @selector(class))
    复制代码
    void objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */)
    
     struct objc_super {
         /// Specifies an instance of a class 
         __unsafe_unretained id receiver;
     }; 
    
    // [super class] => objc_msgSendSuper(super, @selector(class))
    复制代码
    Runtime相关
  • 缓存查找

    • 给定值是SEL,目标值是对应的bucket_t中的IMP

      Runtime相关
  • 当前类中查找

    • 排序 好的列表,采用二分查找算法查找方法对应执行函数
    • 没有排序的列表,采用一般遍历查找方法对应执行函数
  • 父类逐级查找

    Runtime相关
  • 消息转发

    Runtime相关

4. Method-Swizzling

Runtime相关

5. 动态方法添加

+ (BOOL)resolveInstanceMethod:(SEL)sel {

    if (sel == @selector(test)) {
        // 动态添加test方法的实现
        class_addMethod(self, @selector(test), testImp, "v@:");
        return YES;
    } else {
        // 返回父类的默认调用
        return [super resolveInstanceMethod:sel];
    }
}

复制代码

6. 动态方法解析

  • @dynamic

    • 动态运行时语言将函数决议推迟带运行时
    • 编译时语言在编译期进行函数决议

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First JavaScript Programming

Head First JavaScript Programming

Eric T. Freeman、Elisabeth Robson / O'Reilly Media / 2014-4-10 / USD 49.99

This brain-friendly guide teaches you everything from JavaScript language fundamentals to advanced topics, including objects, functions, and the browser’s document object model. You won’t just be read......一起来看看 《Head First JavaScript Programming》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具