iOS:底层原理之 Runtime

Runtime

  • Objective-C 语言是一门动态性比较强的编程语言,跟 C、C++ 有着很大的不同。
  • Objective-C 的动态性是由 Runtime API 来支撑的。
  • Runtime 提供的接口基本是 C 语言的,源码由 C/C++/汇编语言编写。
  • 如果要学习 Runtime ,首先要了解它底层的一些常用的数据结构,如isa指针。
  • 在 arm64 架构之前,isa 就是一个普通的指针,存储着Class、Meta-Class 对象的内存地址。
  • 从 arm64 架构开始,对 isa 进行了优化,变成了一个共用体(union)结构,还使用位域来存储更多的信息。
    在这里插入图片描述
    在这里插入图片描述
  • bits:用来存放所有的数据。

char c1 = -1;
char c2 = 255;
有符号是 -1,无符号是 255,值相等。

共用体
struct {
int year; // 4个字节
int month; // 4个字节
int day; // 4个字节
}
结构体中的三个 int 成员各占4个字节,是独立存在的。

union {
int year;
int month;
int day;
}
大家共用4个字节,共用体所占字节数为最大成员所占字节数。

掩码:MASK,一般用来做位运算。
类对象、元类对象 内存地址的地址值后面三位都是0。

Class 结构

在这里插入图片描述

class_rw_t

class_rw_t 里面的 methods、properties、protocols 是二维数组,是可读可写的,包含了类的初始内容、分类的内容
在这里插入图片描述

class_ro_t

class_ro_t 里面的 baseMethodList、baseProtocols、ivars、baseProperties 是一维数组,是只读的,包含了类的初始内容:
在这里插入图片描述

method_t

是对方法/函数的封装

struct method_t {
    SEL name;         	// 函数名
    const char *types;  // 编码(返回值类型,参数类型)
    MethodListIMP imp;  // 指向函数的指针(函数地址)
}

IMP
IMP 代表函数的具体实现。
在这里插入图片描述
SEL
SEL 代表方法名/函数名,一般叫做选择器。底层结构跟 char * 类似。
可以通过 @selector( ) 和 sel_registerName(“ ”) 获得。
可以通过 sel_getName 和 NSStringFromSelector( ) 转换成字符串。
不同类中相同名字的方法所对应的选择器是相同的(内存地址相同)。
在这里插入图片描述
types
types 包含了函数的返回值类型和参数类型。

Type Encoding

iOS 中提供了一个叫做 @encode 的指令,可以将具体的类型表示成字符串编码:
在这里插入图片描述
例:
-(int)test:(int)age height(float)height;
i24@0:8i16f20
@:id 0
::SEL 8
i:int 16
f:float 20
= 24个字节

方法缓存

cache_t cache
用散列表的方式来缓存曾经调用过的方法,可以提高方法的查找速度。
散列表底层结构是数组。

GSPerson *person = [[GSPerson alloc] init];
[person test];

第一次调用 test 方法时,在找到 test 方法后会将其放到 cache 缓存中。
接下来多次调用此方法,会直接从 cache 缓存中取。

struct cache_t {
    struct bucket_t *_buckets;   //    散列表
    mask_t _mask;                //    散列表的长度 - 1
    mask_t _occupied;            //    已经缓存的方法数量
}

散列表的长度是大于等于已缓存的方法数量的。

struct bucket_t {
private:
    // IMP-first is better for arm64e ptrauth and no worse for arm64.
    // SEL-first is better for armv7* and i386 and x86_64.
#if __arm64__
    uintptr_t _imp;
    SEL _sel;
#else
    SEL _sel;
    uintptr_t _imp;
#endif
}

uintptr_t _imp; // 函数内存地址
SEL _sel; // SEL

objc_msgSend( )

本质:sel_registerName(" ")

objc_msgSend( [GSPerson class], @selector(test) )
消息接受者(receiver): [GSPerson class]
消息名称:test

  • OC中方法调用,其实都是转化为 objc_msgSend 函数的调用
  • objc_msgSend 的执行流程可以分为三大阶段:
    • 消息发送
    • 动态方法解析
    • 消息转发

objc_msgSend 如果找不到合适的方法进行调用,会报错 unrecognized selector send to Instance。

执行流程:在这里插入图片描述
消息发送

在这里插入图片描述

动态方法解析

实例方法

  • (BOOL)resolveInstanceMethod:(SEL)sel

对象方法

  • (BOOL)resolveClassMethod:(SEL)sel
    在这里插入图片描述
消息转发

在这里插入图片描述在这里插入图片描述

动态添加方法

在这里插入图片描述
class_addMethod(<#Class _Nullable __unsafe_unretained cls#>, <#SEL _Nonnull name#>, <#IMP _Nonnull imp#>, <#const char * _Nullable types#>)
四个参数:
#Class
#SEL
#IMP
#const(Types Ecoding)

Runtime API01 – 类

动态创建一个类(参数:父类,类名,额外的内存空间)
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)

注册一个类(要在类注册之前添加成员变量,因为成员变量存放在 ro 中)
void objc_registerClassPair(Class cls)

销毁一个类
void objc_disposeClassPair(Class cls)

获取isa指向的
Class Class object_getClass(id obj)

设置isa指向的
Class Class object_setClass(id obj, Class cls)

判断一个OC对象是否为Class
BOOL object_isClass(id obj)

判断一个Class是否为元类
BOOL class_isMetaClass(Class cls)

获取父类
Class class_getSuperclass(Class cls)

Runtime API02 – 成员变量

获取一个实例变量信息
Ivar class_getInstanceVariable(Class cls, const char *name)

拷贝实例变量列表(最后需要调用free释放)
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)

设置和获取成员变量的值
void object_setIvar(id obj, Ivar ivar, id value) id object_getIvar(id obj, Ivar ivar)

动态添加成员变量(已经注册的类是不能动态添加成员变量的)
BOOL class_addIvar(Class cls, const char * name, size_t size, uint8_t alignment, const char * types)

获取成员变量的相关信息
const char *ivar_getName(Ivar v) const char *ivar_getTypeEncoding(Ivar v)

Runtime API03 – 属性

获取一个属性
objc_property_t class_getProperty(Class cls, const char *name)

拷贝属性列表(最后需要调用free释放)
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)

动态添加属性
BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)

动态替换属性
void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)

获取属性的一些信息
const char *property_getName(objc_property_t property) const char *property_getAttributes(objc_property_t property)

Runtime API04 – 方法

获得一个实例方法、类方法
Method class_getInstanceMethod(Class cls, SEL name)
Method class_getClassMethod(Class cls, SEL name)

方法实现相关操作
IMP class_getMethodImplementation(Class cls, SEL name)
IMP method_setImplementation(Method m, IMP imp)
void method_exchangeImplementations(Method m1, Method m2)

拷贝方法列表(最后需要调用free释放,copy和creat后需要用free释放)
Method *class_copyMethodList(Class cls, unsigned int *outCount)

动态添加方法
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)

动态替换方法
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)

获取方法的相关信息(带有copy的需要调用free去释放,copy和creat后需要用free释放)
SEL method_getName(Method m)
IMP method_getImplementation(Method m)
const char *method_getTypeEncoding(Method m)
unsigned int method_getNumberOfArguments(Method m)
char *method_copyReturnType(Method m)
char *method_copyArgumentType(Method m, unsigned int index)

选择器相关
const char *sel_getName(SEL sel)
SEL sel_registerName(const char *str)

用block作为方法实现
IMP imp_implementationWithBlock(id block)
id imp_getBlock(IMP anImp)
BOOL imp_removeBlock(IMP anImp)

类对象是一个 Class 类型的数据,而 Class 则是一个 objc_class 结构体指针类型的别名。

   typedef struct objc_class * Class;

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值