美文网首页
iOS知识整理-Runtime

iOS知识整理-Runtime

作者: 你好8828 | 来源:发表于2019-12-31 21:42 被阅读0次

Runtime的定义

//对象
struct objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};
//类
struct objc_class {
    Class isa  OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
    Class super_class                                        OBJC2_UNAVAILABLE;
    const char *name                                         OBJC2_UNAVAILABLE;
    long version                                             OBJC2_UNAVAILABLE;
    long info                                                OBJC2_UNAVAILABLE;
    long instance_size                                       OBJC2_UNAVAILABLE;
    struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;
    struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;
    struct objc_cache *cache                                 OBJC2_UNAVAILABLE;
    struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;

isa Class对象,指向objc_class结构体的指针,也就是这个Class的MetaClass(元类)

  • 类的实例对象的 isa 指向该类;该类的 isa 指向该类的 MetaClass
  • MetaCalss的isa对象指向RootMetaCalss
  • 如果MetaClass是RootMetaCalss,那么该MetaClass的isa指针指向它自己

super_class Class对象指向父类对象

  • 如果该类的对象已经是RootClass,那么这个super_class指向nil

  • MetaCalss的SuperClass指向父类的MetaCalss

  • MetaCalss是RootMetaCalss,那么该MetaClass的SuperClass指向该对象的RootClass
    如下图:

    image

ivars: 类中所有属性的列表,使用场景:我们在字典转换成模型的时候需要用到这个列表找到属性的名称,去取字典中的值,KVC赋值,或者直接Runtime赋值

methodLists: 类中所有的方法的列表,类中所有方法的列表,使用场景:如在程序中写好方法,通过外部获取到方法名称字符串,然后通过这个字符串得到方法,从而达到外部控制App已知方法。

cache: 主要用于缓存常用方法列表,每个类中有很多方法,我平时不用的方法也会在里面,每次运行一个方法,都要去methodLists遍历得到方法,如果类的方法不多还行,但是基本的类中都会有很多方法,这样势必会影响程序的运行效率,所以cache在这里就会被用上,当我们使用这个类的方法时先判断cache是否为空,为空从methodLists找到调用,并保存到cache,不为空先从cache中找方法,如果找不到在去methodLists,这样提高了程序方法的运行效率

protocols: 故名思义,这个类中都遵守了哪些协议,使用场景:判断类是否遵守了某个协议上

//方法列表
struct objc_method_list {
    struct objc_method_list *obsolete                        OBJC2_UNAVAILABLE;
    int method_count                                         OBJC2_UNAVAILABLE;
#ifdef __LP64__
    int space                                                OBJC2_UNAVAILABLE;
#endif
    /* variable length structure */
    struct objc_method method_list[1]                        OBJC2_UNAVAILABLE;
}                                                            OBJC2_UNAVAILABLE;
//方法
struct objc_method {
    SEL method_name                                          OBJC2_UNAVAILABLE;
    char *method_types                                       OBJC2_UNAVAILABLE;
    IMP method_imp                                           OBJC2_UNAVAILABLE;
}

Runtime消息转发

调用方法时,先消息传递,没搜到再转发,没有转发就用崩溃。
消息传递:分类(先缓存表后方法表)->当前类->父类->NSObject
消息转发:动态方法解析->备用接收者->完整消息转发

image

Runtime常用方法

// 动态获取类中的所有属性(包括私有) 
Ivar *ivar = class_copyIvarList(_person.class, &count);
// 修改对应的字段值
object_setIvar(_person, tempIvar, @"更改属性值成功");
//动态添加方法
class_addMethod([_person class], @selector(coding), (IMP)codingOC, "v@:");

//方法交换(避免崩溃)
// 判断自定义的方法是否实现, 
BOOL addSuccess = class_addMethod(self.class, @selector(sendAction:to:forEvent:), method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
if (addSuccess) {
    // 没有实现, 将源方法的实现替换到交换方法的实现
    class_replaceMethod(self.class, @selector(customSendAction:to:forEvent:), method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
}else {
     // 已经实现, 直接交换方法
     method_exchangeImplementations(oriMethod, cusMethod);
}

Runtime应用

关联对象(Objective-C Associated Objects)给分类增加属性
方法魔法(Method Swizzling)方法添加和替换和KVO实现
消息转发(热更新)解决Bug(JSPatch)
实现NSCoding的自动归档和自动解档
实现字典和模型的自动转换(MJExtension)

相关文章

  • iOS知识点整理

    iOS知识点整理 持续更新中。。。 runtime相关 iOS 模块详解—「Runtime面试、工作」看我就 ? ...

  • iOS知识整理-Runtime

    Runtime的定义 isa Class对象,指向objc_class结构体的指针,也就是这个Class的Meta...

  • iOS知识整理-Runtime

    Runtime的定义 isa Class对象,指向objc_class结构体的指针,也就是这个Class的Meta...

  • iOS 常见知识点(一):Runtime

    iOS 常见知识点(二):RunLoop iOS 常见知识点(三):Lock Runtime Runtime 是一...

  • iOS开发·runtime原理与实践: 基本知识篇

    iOS开发·runtime原理与实践: 基本知识篇 iOS开发·runtime原理与实践: 基本知识篇

  • iOS runtime 笔记总览

    个人已掌握的知识点 runtime的一些综述iOS runtime笔记一iOS runtime 笔记二 — Met...

  • Runtime-iOS运行时基础篇

    本文主要整理了Runtime的相关知识。对于一个iOS开发者来说,掌握Runtime的重要性早已不言而喻。OC能够...

  • iOS Runtime

    简书内容都是个人的知识点整理和笔记。 iOS Runtime理解是我们每一个iOS开发者在深刻掌握Objectiv...

  • iOS RunTime 详解

    本文讲述 iOS Runtime 相关的知识点,从下面几个方面探寻 iOS Runtime的实现机制。 Runti...

  • iOS知识点整理-Runtime

    前言:我是一个有着两年开发经验的iOS开发者,经过前段时间的试水,从面试中认识到了自己的不足。所以就把面试中遇到的...

网友评论

      本文标题:iOS知识整理-Runtime

      本文链接:https://www.haomeiwen.com/subject/axbioctx.html