3、聊聊iOS中的Runtime

1、Runtime是什么?

我们知道啊,在oc 中 之所以称之为动态语言,是因为有Runtime这一运行库,它使一套底层的C语言框架库,如果你想了解更多的运行时的底层实现,您可以移步Runtime源码,这里面有更多关于Runtime的底层实现,如果有C , C++ ,汇编基础建议你查看,其中objc_msgSend就是使用汇编实现的,如下


/////////////////////////////////////////////////////////////////////
//
// CacheLookup  return-type
//
// Locate the implementation for a selector in a class's method cache.
//
// Takes: 
//    $0 = NORMAL, STRET, SUPER, SUPER_STRET, SUPER2, SUPER2_STRET, GETIMP
//    r0 or r1 (STRET) = receiver
//    r1 or r2 (STRET) = selector
//    r9 = class to search in
//
// On exit: r9 and r12 clobbered
//      (found) calls or returns IMP, eq/ne/r9 set for forwarding
//      (not found) jumps to LCacheMiss
//
/////////////////////////////////////////////////////////////////////
    
.macro CacheHit

.if $0 == GETIMP //将0 与GETIMP变量比较
    ldr r0, [r9, #4]        // r0 = bucket->imp ,从r9寄存器中取出后四位地址值,放到 r0中
    MI_GET_ADDRESS(r1, __objc_msgSend_uncached_impcache) ;这里会传递一个函数指针的地址近来
    teq r0, r1 //测试比较 r0 和 r1 是否相等,可以理解为 if ( r0 == r1) 测试是否等于
    it  eq // it 是if than 的缩写 , eq 是equal 缩写
    moveq   r0, #0          // don't return msgSend_uncached , 拆开理解 ,move equal 将 r0 寄存器中的值 与 0 进行比较,如果不为0  ,则设置为0
    bx  lr          // return imp //跳转到lr 连接寄存器(Link Register, LR)
.elseif $0 == NORMAL
    ldr r12, [r9, #4]       // r12 = bucket->imp
                    // eq already set for nonstret forward
    MESSENGER_END_FAST
    bx  r12         // call imp
.elseif $0 == STRET
    ldr r12, [r9, #4]       // r12 = bucket->imp
    movs    r9, #1          // r9=1, Z=0 for stret forwarding
    MESSENGER_END_FAST
    bx  r12         // call imp
.elseif $0 == SUPER
    ldr r12, [r9, #4]       // r12 = bucket->imp
    ldr r9, [r0, #CLASS]    // r9 = class to search for forwarding
    ldr r0, [r0, #RECEIVER] // fetch real receiver
    tst r12, r12        // set ne for forwarding (r12!=0)
    MESSENGER_END_FAST
    bx  r12         // call imp
.elseif $0 == SUPER2
    ldr r12, [r9, #4]       // r12 = bucket->imp
    ldr r9, [r0, #CLASS]
    ldr r9, [r9, #SUPERCLASS]   // r9 = class to search for forwarding
    ldr r0, [r0, #RECEIVER] // fetch real receiver
    tst r12, r12        // set ne for forwarding (r12!=0)
    MESSENGER_END_FAST
    bx  r12         // call imp
.elseif $0 == SUPER_STRET
    ldr r12, [r9, #4]       // r12 = bucket->imp
    ldr r9, [r1, #CLASS]    // r9 = class to search for forwarding
    orr r9, r9, #1      // r9 = class|1 for super_stret forward
    ldr r1, [r1, #RECEIVER] // fetch real receiver
    tst r12, r12        // set ne for forwarding (r12!=0)
    MESSENGER_END_FAST
    bx  r12         // call imp
.elseif $0 == SUPER2_STRET
    ldr r12, [r9, #4]       // r12 = bucket->imp
    ldr r9, [r1, #CLASS]    // r9 = class to search for forwarding
    ldr r9, [r9, #SUPERCLASS]   // r9 = class to search for forwarding
    orr r9, r9, #1      // r9 = class|1 for super_stret forward
    ldr r1, [r1, #RECEIVER] // fetch real receiver
    tst r12, r12        // set ne for forwarding (r12!=0)
    MESSENGER_END_FAST
    bx  r12         // call imp
.else
.abort oops
.endif

.endmacro


  • 2、上面的汇编代码,能看懂大概就行,没必要深究,配上消息机制图或许好理解些
4196_131216091606_1.png
  • 3 消息机制详解请移步这里

2、Runtime 头文件 简单介绍

因为头文件篇幅比较长,所以没有再此展示,请移步runtime头文件简要翻译

3 Runtime 用途

  • 1、字典转模型
  • 2 、解档归档优化代码
  • 3 、给category添加属性
  • 4、获取系统私有成员变量及其方法
  • 5、使用Method Swizzling 实现偷梁换柱,怎么使用您可以移步Method Swizzling

Runtime 相关学习链接

1、南峰子的技术博客
2、runtime 装逼指南

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,872评论 0 9
  • 本文转载自:http://yulingtianxia.com/blog/2014/11/05/objective-...
    ant_flex阅读 819评论 0 1
  • 转载:http://yulingtianxia.com/blog/2014/11/05/objective-c-r...
    F麦子阅读 791评论 0 2
  • 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的...
    西木阅读 30,697评论 33 466
  • 01 在盗笔铁三角中,小哥是武力值担当,那可是盗笔世界中站在冰箱上俯瞰众...
    冷话阅读 535评论 0 2