更多文章请点击下方:
介绍
Objective-C 语言将决定尽可能的从编译和链接时推迟到运行时。只要有可能,Objective-C 总是使用动态的方式来解决问题。这意味着 Objective-C 语言不仅需要一个编译器,同时也需要一个运行时系统来执行 编译好的代码。这儿的运行时系统扮演的角色类似于 Objective-C 语言的操作系统,Objective-C 基于该系统来工作。
Runtime是由C、C++、汇编实现的底层库,用于支持Objective-C语言的动态特性。我们使用Objective-C创建类或对象都是通过Runtime来实现消息发送、消息转发的。
如下图:
上图中Runtime System Library 就是Runtime的底层,经过编译过,会提供Runtime API、Framework和Service ,最终会提供给相应的Objective-C使用。
Runtime提供的API,请点击查看Objective-C Runtime
平时我们编写代码并运行程序都要先将Objective-C语言转换成C语言,C语言再转换成汇编语言,汇编语言也编译成机器可以识别的二进制,中间的转换过程就是由Runtime来实现的,程序进行起来后,会装载到内存中,通过Runtime提供运行时的功能。
版本
Runtime有两个版本:
- Modern (Objective-C 2.0常用版本)
- Legacy (早期版本)
版本区别:当更改类的实例变量的布局时,Legacy需要重新编译它的子类,Modern则不用
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.
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 in The Objective-C Programming cLanguage).