Objective-C 扩展了 C 语言,并加入了面向对象特性和 Smalltalk 式的消息传递机制。而这个扩展的核心是一个用 C 和 编译语言 写的 Runtime 库。它是 Objective-C 面向对象和动态机制的基石。html
Objective-C 是一个动态语言,这意味着它不只须要一个编译器,也须要一个运行时系统来动态得建立类和对象、进行消息传递和转发。理解 Objective-C 的 Runtime 机制能够帮咱们更好的了解这个语言,适当的时候还能对语言进行扩展,从系统层面解决项目中的一些设计或技术问题。一句话: 学好Runtime , iOS躺着走git
# Runtime Versions and Platforms
There are different versions of the Objective-C runtime on different platforms.
## Legacy and Modern Versions
There are two versions of the Objective-C runtime—“modern” and “legacy”. The modern version was introduced with Objective-C 2.0 and includes a number of new features. The programming interface for the legacy version of the runtime is described in *Objective-C 1 Runtime Reference*; the programming interface for the modern version of the runtime is described in *[Objective-C Runtime Reference](https://developer.apple.com/documentation/objectivec/objective_c_runtime)*.
The most notable new feature is that instance variables in the modern runtime are “non-fragile”:
* In the legacy runtime, if you change the layout of instance variables in a class, you must recompile classes that inherit from it.
* In the modern runtime, if you change the layout of instance variables in a class, you do not have to recompile classes that inherit from it.
In addition, the modern runtime supports instance variable synthesis for declared properties (see [Declared Properties](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17) in *[The Objective-C Programming Language](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163)*).
## Platforms
iPhone applications and 64-bit programs on OS X v10.5 and later use the modern version of the runtime.
Other programs (32-bit programs on OS X desktop) use the legacy version of the runtime.
复制代码
Runtime
其实有两个版本: “modern
” 和 “legacy
”。咱们如今用的 Objective-C 2.0
采用的是现行 (Modern
) 版的 Runtime
系统,只能运行在 iOS
和 macOS 10.5
以后的 64
位程序中。而 macOS
较老的32
位程序仍采用 Objective-C 1
中的(早期)Legacy
版本的 Runtime
系统。这两个版本最大的区别在于当你更改一个类的实例变量的布局时,在早期版本中你须要从新编译它的子类,而现行版就不须要。github
Runtime
基本是用 C
和汇编
写的,可见苹果为了动态系统的高效而做出的努力。你能够在这里下到苹果维护的开源代码。苹果和GNU各自维护一个开源的 runtime/GNUStep 版本,这两个版本之间都在努力的保持一致。objective-c
平时的业务中主要是使用官方Api,解决咱们框架性的需求。编程
高级编程语言
想要成为可执行文件须要先编译为汇编语言再汇编为机器语言,机器语言也是计算机可以识别的惟一语言,可是OC
并不能直接编译为汇编语言,而是要先转写为纯C
语言再进行编译和汇编的操做,从OC
到C
语言的过渡就是由runtime来实现的。然而咱们使用OC
进行面向对象开发,而C
语言更多的是面向过程开发,这就须要将面向对象的类转变为面向过程的结构体。缓存
OK 咱们先来看看与runtime 交互的三种方式:bash
OC 原生底层就是runtime 会在后台执行 好比方法的实质就是消息 对于大多数状况下,OC运行时系统自动的在后台运行。你只需编写和编译OC代码就能使用它。 当你编译包含OC类和方法的代码时,编译器建立用来实现语言动态特性的数据结构体和方法调用。数据结构获取类和类定义的信息和协议中定义的信息,包含了在《The Objective-C Programming Language》中对“ Defining a Class and Protocols”谈论的类和协议的对象,以及方法选择,实例变量模版,和其余葱源代码中提取出来的信息。运行时主要的一个功能是发送消息,正如在Messaging 中的描述。它是由源代码的消息表达式调用的。数据结构
经过调用NSObject
的方法 间接调用runtimeapp
+ (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
这个方法里面篇幅很长里面介绍了如下几点:
里面还有关于
runtimeLock
运行时锁,这里加锁了read()
对读取,其中runtimeLock
是经过pthread_rwlock_t
实现的,更加底层的,你们若是感兴趣锁能够参考这篇互斥锁-读写锁-条件锁
以上设计了消息
,动态方法解析
,还有消息转发
,咱们在接下来的篇幅中还会更加深刻研究.咱们继续回来,第三种runtime
交互
runtime
的API