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
如下图:
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
消息转发:动态方法解析->备用接收者->完整消息转发
〜
〜
〜
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 Runtime详解
参考:深入浅出Runtime (一) 什么是Runtime?
参考:新手也看得懂的 iOS Runtime 教程