runtime理解

runtime是一套可以进行一些非常底层的用OC无法实现的操作的纯C语言的API。

点击进入官方文档

Objective-C类由一个指向objc_class结构体的指针来表示

//An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;

objc/runtime.h中objc_class结构体的定义如下:

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;

一个类的实例由结构体objc_object来表示

//Represents an instance of a class.
struct objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};

//A pointer to an instance of a class.
typedef struct objc_object *id;

当我们向一个Objective-C对象发送消息时,运行时库会根据实例对象的isa指针找到这个实例对象所属的类。Runtime库会在类或其父类的方法列表中查询并执行与消息对应的selector指向的方法。

元类(MetaClass)是一个类对象的类

(所有的类自身也是一个对象,我们可以向这个对象发送消息—调用类方法。)

meta-class也是一个类,也可以向它发送一个消息。Objective-C的设计者让所有的meta-class的isa指向基类的meta-class,以此作为它们的所属类。即,任何NSObject继承体系下的meta-class都使用NSObject的meta-class作为自己的所属类,而基类的meta-class的isa指针是指向它自己。这样就形成了一个完美的闭环。

UIView *testView = [[UIView alloc] init];

NSLog(@"%p", self);
NSLog(@"%s", class_getName([testView class] ));
NSLog(@"class is %s, superclass is %s", class_getName([self class]),
      class_getName([self superclass]));

Class currentClass = [self class];
for (int i = 0; i < 3; i++) {
    NSLog(@"Following the isa pointer %d times gives %p", i, currentClass);
    currentClass = objc_getClass((__bridge void *)currentClass);
}

控制台输出:

0x7fa410c1f990
UIView
class is ViewController, superclass is UIViewController
Following the isa pointer 0 times gives 0x101c4e6c0
Following the isa pointer 1 times gives 0x0
Following the isa pointer 2 times gives 0x0

runtime对类和对象的操作

runtime提供了大量的函数来操作类与对象。类的操作方法大部分是以class_为前缀的,而对象的操作方法大部分是以objc_或object_为前缀。

对类操作

runtime提供的操作类的方法主要是针对结构体objc_class中的各个字段的。

//Returns the name of a class.
const char * class_getName(Class cls);
//Returns the superclass of a class.
Class class_getSuperclass(Class cls);
//Sets the superclass of a given class, return the old superclass for cls.
Class class_setSuperclass(Class cls, Class newSuper);
//Returns a Boolean value that indicates whether a class object is a metaclass.
BOOL class_isMetaClass(Class cls);
//Returns the size of instances of a class.
size_t class_getInstanceSize(Class cls);
//Returns the Ivar for a specified instance variable of a given class.
Ivar class_getInstanceVariable(Class cls, const char* name);
//Returns the Ivar for a specified class variable of a given class.
Ivar class_getClassVariable(Class cls, const char* name);
//Returns a description of the Ivar layout for a given class.
const char *class_getIvarLayout(Class cls);
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值