runtime简介
Objective-C语言尽可能多地推迟从编译时间和链接时间到运行时的决策。只要有可能,它就会动态地做事。这意味着该语言不仅需要编译器,还需要运行时系统来执行编译的代码。运行时系统充当Objective-C语言的一种操作系统;它是使该语言发挥作用的原因。
本文研究NSObject类以及Objective-C程序如何与运行时系统交互。特别是,它检查了在运行时动态加载新类并将消息转发到其他对象的范式。它还提供了如何在程序运行时查找对象信息的信息。
没有比官方文档最有说服力的文档了,点这里就是这里# Objective-C运行时编程指南
小知识点: 编译时:编译器将源代码(高级语言)翻译成机器语言(如汇编)/二进制
运行时:代码运行时,会被装载到内存,
Objective-C 是一个动态语言,这意味着它不仅需要一个编译器,也需要一个运行时系统来动态得创建类和对象、进行消息传递和转发。理解 Objective-C 的 Runtime 机制可以帮我们更好的了解这个语言,适当的时候还能对语言进行扩展,从系统层面解决项目中的一些设计或技术问题。一句话: 学好Runtime , iOS躺着走
Runtime其实有两个版本: “modern” 和 “legacy”。我们现在用的 Objective-C 2.0 采用的是现行 (Modern) 版的 Runtime 系统,只能运行在 iOS 和 macOS 10.5 之后的 64 位程序中。而 macOS 较老的32位程序仍采用 Objective-C 1 中的(早期)Legacy 版本的 Runtime 系统。这两个版本最大的区别在于当你更改一个类的实例变量的布局时,在早期版本中你需要重新编译它的子类,而现行版就不需要。
Runtime 基本是用 C 和汇编写的,可见苹果为了动态系统的高效而作出的努力。你可以在这里下到苹果维护的开源代码。苹果和GNU各自维护一个开源的 runtime/GNUStep 版本,这两个版本之间都在努力的保持一致。
平时的业务中主要是使用官方Api,解决我们框架性的需求。
高级编程语言想要成为可执行文件需要先编译为汇编语言再汇编为机器语言,机器语言也是计算机能够识别的唯一语言,但是OC并不能直接编译为汇编语言,而是要先转写为纯C语言再进行编译和汇编的操作,从OC到C语言的过渡就是由runtime来实现的。然而我们使用OC进行面向对象开发,而C语言更多的是面向过程开发,这就需要将面向对象的类转变为面向过程的结构体。
详细看这里
我们先来看看与runtime 交互的三种方式:
- OC 原生底层就是runtime 会在后台执行 比如方法的实质就是消息
对于大多数情况下,OC运行时系统自动的在后台运行。你只需编写和编译OC代码就能使用它。
当你编译包含OC类和方法的代码时,编译器创建用来实现语言动态特性的数据结构体和方法调用。数据结构获取类和类定义的信息和协议中定义的信息,包含了在《The Objective-C Programming Language》中对“ Defining a Class and Protocols”谈论的类和协议的对象,以及方法选择,实例变量模版,和其他葱源代码中提取出来的信息。运行时主要的一个功能是发送消息,正如在Messaging 中的描述。它是由源代码的消息表达式调用的。
1. 通过调用NSObject的方法 间接调用runtime
+ (BOOL)isSubclassOfClass:(Class)aClass;
+ (BOOL)instancesRespondToSelector:(SEL)aSelector;
+ (BOOL)conformsToProtocol:(Protocol *)protocol;
- (IMP)methodForSelector:(SEL)aSelector;
+ (IMP)instanceMethodForSelector:(SEL)aSelector;
- (void)doesNotRecognizeSelector:(SEL)aSelector;
- (BOOL)isKindOfClass:(Class)aClass;
- (BOOL)isMemberOfClass:(Class)aClass;
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;
- (BOOL)respondsToSelector:(SEL)aSelector;
这里给大家解释一下: 以上方法都是在运行时会编译成响应的方法:比如- (BOOL)respondsToSelector:(SEL)aSelector 我们看编译会来到objc 的这里
BOOL class_respondsToSelector(Class cls, SEL sel)
{
return class_respondsToSelector_inst(cls, sel, nil);
}
//继续跟踪 看到回来到下面的方法 ,会去查找当前sel 对应的imp是否存在
bool class_respondsToSelector_inst(Class cls, SEL sel, id inst)
{
IMP imp;
if (!sel || !cls) return NO;
// Avoids +initialize because it historically did so.
// We're not returning a callable IMP anyway.
imp = lookUpImpOrNil(cls, sel, inst,
NO/*initialize*/, YES/*cache*/, YES/*resolver*/);
return bool(imp);
}
//下面这里就是真正去查找imp的方法,我会在注重介绍一下
IMP lookUpImpOrNil(Class cls, SEL sel, id inst,
bool initialize, bool cache, bool resolver)
{
IMP imp = lookUpImpOrForward(cls, sel, inst, initialize, cache, resolver);
if (imp == _objc_msgForward_impcache) return nil;
else return imp;
}
上面的两部跳动,都是给下面的方法做铺垫的,下面的方法也runtime非常重要的方法,下面我们花点篇幅介绍一下
IMP lookUpImpOrForward(Class cls, SEL sel, id inst,
bool initialize, bool cache, bool resolver)
{
IMP imp = nil;
bool triedResolver = NO;
runtimeLock.assertUnlocked();
// 如果cache是YES,则从缓存中查找IMP。
if (cache) {
// 通过cache_getImp函数查找IMP,查找到则返回IMP并结束调用
imp = cache_getImp(cls, sel);
if (imp) return imp;
}
runtimeLock.read();
// 判断类是否已经被创建,如果没有被创建,则将类实例化
if (!cls->isRealized()) {
// Drop the read-lock and acquire the write-lock.
// realizeClass() checks isRealized() again to prevent
// a race while the lock is down.
runtimeLock.unlockRead();
runtimeLock.write();
// 对类进行实例化操作
realizeClass(cls);
runtimeLock.unlockWrite();
runtimeLock.read();
}
// 第一次调用当前类的话,执行initialize的代码
if (initialize && !cls->isInitialized()) {
runtimeLock.unlockRead();
// 对类进行初始化,并开辟内存空间
_class_initialize (_class_getNonMetaClass(cls, inst));
runtimeLock.read();
// If sel == initialize, _class_initialize will send +initialize and
// then the messenger will send +initialize again after this
// procedure finishes. Of course, if this is not being called
// from the messenger then it won't happen. 2778172
}
retry:
runtimeLock.assertReading();
// 尝试获取这个类的缓存
imp = cache_getImp(cls, sel);
if (imp) goto done;
{
// 如果没有从cache中查找到,则从方法列表中获取Method
Method meth = getMethodNoSuper_nolock(cls, sel);
if (meth) {
// 如果获取到对应的Method,则加入缓存并从Method获取IMP
log_and_fill_cache(cls, meth->imp, sel, inst, cls);
imp = meth->imp;
goto done;
}
}
// Try superclass caches and method lists.
{
unsigned attempts = unreasonableClassCount();
// 循环获取这个类的缓存IMP 或 方法列表的IMP
for (Class curClass = cls->superclass;
curClass != nil;
curClass = curClass->superclass)
{
// Halt if there is a cycle in the superclass chain.
if (--attempts == 0) {
_objc_fatal("Memory corruption in class list.");
}
// Superclass cache.
// 获取父类缓存的IMP
imp = cache_getImp(curClass, sel);
if (imp) {
if (imp != (IMP)_objc_msgForward_impcache) {
// Found the method in a superclass. Cache it in this class.
// 如果发现父类的方法,并且不再缓存中,在下面的函数中缓存方法
log_and_fill_cache(cls, imp, sel, inst, curClass);
goto done;
}
else {
// Found a forward:: entry in a superclass.
// Stop searching, but don't cache yet; call method
// resolver for this class first.
break;
}
}
// Superclass method list.
// 在父类的方法列表中,获取method_t对象。如果找到则缓存查找到的IMP
Method meth = getMethodNoSuper_nolock(curClass, sel);
if (meth) {
log_and_fill_cache(cls, meth->imp, sel, inst, curClass);
imp = meth->imp;
goto done;
}
}
}
// No implementation found. Try method resolver once.
// 如果没有找到,则尝试动态方法解析
if (resolver && !triedResolver) {
runtimeLock.unlockRead();
_class_resolveMethod(cls, sel, inst);
runtimeLock.read();
// Don't cache the result; we don't hold the lock so it may have
// changed already. Re-do the search from scratch instead.
triedResolver = YES;
goto retry;
}
// No implementation found, and method resolver didn't help.
// Use forwarding.
// 如果没有IMP被发现,并且动态方法解析也没有处理,则进入消息转发阶段
imp = (IMP)_objc_msgForward_impcache;
cache_fill(cls, sel, imp, inst);
done:
runtimeLock.unlockRead();
return imp;
}
lookUpImpOrForward这个方法里面篇幅很长里面介绍了以下几点:
- 如果cache是YES,则从缓存中查找IMP。这里也就是说我们如果之前响应过的,在cache存过,就不需要下面的操作了
- 判断类是否已经被创建,如果没有被创建,则将类实例化
- 第一次调用当前类的话,执行initialize的代码
- 尝试获取这个类的缓存 (这里很多小伙伴就会质疑,为什么还要取一次内存,要知道OC是动态语言,在我们执行这个获取imp的时候,外界在开锁,解锁的时候是可以访问的,动态操作)
- 如果没有从cache中查找到,则从方法列表中获取Method
- 如果还没有,就从父类缓存或者方法列表获取imp
- 如果没有找到,则尝试动态方法解析
- 如果没有IMP被发现,并且动态方法解析也没有处理,则进入消息转发阶段
里面还有关于runtimeLock运行时锁,这里加锁了read()对读取,其中runtimeLock是通过pthread_rwlock_t实现的,更加底层的,大家如果感兴趣锁可以参考这篇互斥锁-读写锁-条件锁
以上设计了消息,动态方法解析,还有消息转发,我们在接下来的篇幅中还会更加深入研究.我们继续回来,第三种runtime交互
- 直接调用
runtime的API
IPA的介绍篇幅,我们就放到下一篇.
iOS底层原理(二):Runtime研究(一) rumtime-简介说明
API
这个篇幅我们继续研究
runtime,这里给大家介绍runtime的api
- Working with Classes
- Adding Classes
- Instantiating Classes
- Working with Instances
- Obtaining Class Definitions
- Working with Instance Variables
- Associative References
- Sending Messages
- Working with Methods
- Working with Libraries
- Working with Selectors
- Working with Protocols
- Working with Properties
- Using Objective-C Language Features
- Class-Definition Data Structures
- Instance Data Types
- Boolean Value
- Associative References
- Constants
- objc_系列函数关注于宏观使用,如类与协议的空间分配,注册,注销等操作
-
class_系列函数关注于类的内部,如实例变量,属性,方法,协议等相关问题
-
objcet_系列函数关注于对象的角度,如实例变量 还想看 看这里
runtime版本
优秀文章
iOS底层原理(二):Runtime研究(一) rumtime-简介说明
iOS底层原理(二):Runtime研究(二) runtime-api文档说明