很多学习Object-C的小伙伴经常花大部分时间去学习OC的语法、Cocoa框架、以及第三方的框架,以至于忽略了OC的runtime特性,也有许多小伙伴也是想去了解但是发现概念过于难以理解,我就以功能进行Runtime的介绍,希望能够对你有所帮助。
Runtime是什么?
Objective-C 的 Runtime 是一个运行时库(Runtime Library),它是一个主要使用 C 和汇编写的库,为 C 添加了面相对象的能力并创造了 Objective-C。很多人都知道OC是一个面向对象的动态语言,这正是Runtime保证了这一点。
Runtime能干什么?
对类的操作
动态创建一个新的类:
Class objc_allocateClassPair(Class superclass, const char *name,size_t extraBytes)
销毁一个类:
void objc_disposeClassPair(Class cos)
对于变量/属性的操作
获取类中指定名称实例成员变量的信息
Ivar class_getInstanceVariable ( Class cls, const char *name );
获取类成员变量的信息
Ivar class_getClassVariable ( Class cls, const char *name );
添加成员变量
BOOL class_addIvar ( Class cls, const char *name, size_t size, uint8_t alignment, const char *types );
获取整个成员变量列表
Ivar * class_copyIvarList ( Class cls, unsigned int *outCount );
获取指定的属性
objc_property_t class_getProperty ( Class cls, const char *name );
获取属性列表
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 );
对于方法的操作:
执行方法
id objc_msgSend(id self, SEL op, ...)
执行父类的方法
id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
添加方法
BOOL class_addMethod ( Class cls, SEL name, IMP imp, const char *types );
获取实例方法
Method class_getInstanceMethod ( Class cls, SEL name );
获取类方法
Method class_getClassMethod ( Class cls, SEL name );
获取所有方法列表
Method * class_copyMethodList ( Class cls, unsigned int *outCount );
替代方法的实现
IMP class_replaceMethod ( Class cls, SEL name, IMP imp, const char *types );
返回方法的具体实现
IMP class_getMethodImplementation ( Class cls, SEL name );
IMP class_getMethodImplementation_stret ( Class cls, SEL name );
类实例是否响应指定的selector
BOOL class_respondsToSelector ( Class cls, SEL sel );
关于协议的操作:
添加协议
BOOL class_addProtocol ( Class cls, Protocol *protocol );
返回类是否实现指定的协议
BOOL class_conformsToProtocol ( Class cls, Protocol *protocol );
返回类实现的协议列表
Protocol * class_copyProtocolList ( Class cls, unsigned int *outCount );
与字符串间的转化操作
将方法转化为字符串
NSString *NSStringFromSelector(SEL aSelector);
将字符串转化为方法
SEL NSSelectorFromString(NSString *aSelectorName);
将类转化为字符串
NSString *NSStringFromClass(Class aClass);
将字符串转化为类
Class __nullable NSClassFromString(NSString *aClassName);
将协议转化为字符串
NSString *NSStringFromProtocol(Protocol *proto) NS_AVAILABLE(10_5, 2_0);
将字符串转化为协议
Protocol * __nullable NSProtocolFromString(NSString *namestr) NS_AVAILABLE(10_5, 2_0);
Runtime在系统中的体现
系统的相关定义
super_class:指向该类的父类,如果该类已经是最顶层的根类(如NSObject或NSProxy),则super_class为NULL。
name:当前类的名字。可以通过class_getName(Class cls)进行获取
version:类当前版本。可以通过class_getVersion(Class cls)进行获取,通过class_setVersion(Class cls,int version)进行设置,我们可以通过他识别出不同版本的类的改变 。
cache:用于缓存最近使用的方法。一个接收者对象接收到一个消息时,它会根据isa指针去查找能够响应这个消息的对象。在实际使用中,这个对象只有一部分方法是常用的,很多方法其实很少用或者根本用不上。这种情况下,如果每次消息来时,我们都是methodLists中遍历一遍,性能势必很差。这时,cache就派上用场了。在我们每次调用过一个方法后,这个方法就会被缓存到cache列表中,下次调用的时候runtime就会优先去cache中查找,如果cache没有,才去methodLists中查找方法。这样,对于那些经常用到的方法的调用,但提高了调用的效率。
ivars:成员链表,我们可以通过该指针获取类的所有的成员变量。
methodList:方法链表,我们可以通过该指针获取类的所有方法。
protocols:协议链表,我们可以通过该指针获取类的所有协议。