runtime

作者: 找个地方记录点东西 | 来源:发表于2016-07-07 10:17 被阅读13次

1idclass指针

class指针只向objc_class结构体

id指针只向objc_object结构体

isa是一个指向objc_class结构体(该对象所属的类)

id是对象,class是类

struct objc_object {

Class isaOBJC_ISA_AVAILABILITY;

};

objc_class的定义如下

typedef struct objc_class *Class;

struct objc_class {

Class isaOBJC_ISA_AVAILABILITY; // metaclass

#if !__OBJC2__

Class super_classOBJC2_UNAVAILABLE; //父类

const char *nameOBJC2_UNAVAILABLE; //类名

long versionOBJC2_UNAVAILABLE; //类的版本信息,默认为0,可以通过runtime函数class_setVersion或者class_getVersion进行修改、读取

long infoOBJC2_UNAVAILABLE; //类信息,供运行时期使用的一些位标识,如CLS_CLASS (0x1L)表示该类为普通class,其中包含实例方法和变量;CLS_META (0x2L)表示该类为metaclass,其中包含类方法;

long instance_sizeOBJC2_UNAVAILABLE; //该类的实例变量大小(包括从父类继承下来的实例变量)

struct objc_ivar_list *ivarsOBJC2_UNAVAILABLE; //该类的成员变量地址列表

struct objc_method_list **methodListsOBJC2_UNAVAILABLE; //方法地址列表,与info的一些标志位有关,如CLS_CLASS (0x1L),则存储实例方法,如CLS_META (0x2L),则存储类方法;

struct objc_cache *cacheOBJC2_UNAVAILABLE; //缓存最近使用的方法地址,用于提升效率;

struct objc_protocol_list *protocolsOBJC2_UNAVAILABLE; //存储该类声明遵守的协议的列表

#endif

}

2SEL

SEL是selector在Objective-C中的表示类型。selector可以理解为区别方法的ID。

typedef struct objc_selector *SEL;

struct objc_selector {

char *name;OBJC2_UNAVAILABLE;//名称

char *types;OBJC2_UNAVAILABLE;//类型

};

3IMP

typedef id (*IMP)(id, SEL, ...);

IMP是“implementation”的缩写,它是由编译器生成的一个函数指针。当你发起一个消息后(下文介绍),这个函数指针决定了最终执行哪段代码。

4Method

Method代表类中的某个方法的类型。

typedef struct objc_method *Method;

struct objc_method {

SEL method_nameOBJC2_UNAVAILABLE; //方法名

char *method_typesOBJC2_UNAVAILABLE; //方法类型

IMP method_impOBJC2_UNAVAILABLE; //方法实现

}

方法名method_name类型为SEL,

方法类型method_types是一个char指针,存储着方法的参数类型和返回值类型。

方法实现method_imp的类型为IMP

5Ivar

Ivar代表类中实例变量的类型

typedef struct objc_ivar *Ivar;

struct objc_ivar {

char *ivar_nameOBJC2_UNAVAILABLE; //变量名

char *ivar_typeOBJC2_UNAVAILABLE; //变量类型

int ivar_offsetOBJC2_UNAVAILABLE; //基地址偏移字节

#ifdef __LP64__

int spaceOBJC2_UNAVAILABLE; //占用空间

#endif

}

6objc_property_t

objc_property_t是属性,它的定义如下:

typedef struct objc_property *objc_property_t;

objc_property是内置的类型,与之关联的还有一个objc_property_attribute_t,它是属性的attribute,也就是其实是对属性的详细描述,包括属性名称、属性编码类型、原子类型/非原子类型等。它的定义如下:

typedef struct {

const char *name; //名称

const char *value;//值(通常是空的)

} objc_property_attribute_t;

7Cache

Catch的定义如下:

typedef struct objc_cache *Cache

struct objc_cache {

unsigned int maskOBJC2_UNAVAILABLE;

unsigned int occupiedOBJC2_UNAVAILABLE;

Method buckets[1]OBJC2_UNAVAILABLE;

};

mask:指定分配cache buckets的总数。在方法查找中,Runtime使用这个字段确定数组的索引位置。

occupied:实际占用cache buckets的总数。

buckets:指定Method数据结构指针的数组。这个数组可能包含不超过mask+1个元素。需要注意的是,指针可能是NULL,表示这个缓存bucket没有被占用,另外被占用的bucket可能是不连续的。这个数组可能会随着时间而增长。

objc_msgSend(下文讲解)每调用一次方法后,就会把该方法缓存到cache列表中,下次的时候,就直接优先从cache列表中寻找,如果cache没有,才从methodLists中查找方法。

8Catagory

这个就是我们平时所说的类别了。它可以动态的为已存在的类添加新的方法

typedef struct objc_category *Category;

struct objc_category {

char *category_nameOBJC2_UNAVAILABLE; //类别名称

char *class_nameOBJC2_UNAVAILABLE; //类名

struct objc_method_list *instance_methodsOBJC2_UNAVAILABLE; //实例方法列表

struct objc_method_list *class_methodsOBJC2_UNAVAILABLE; //类方法列表

struct objc_protocol_list *protocolsOBJC2_UNAVAILABLE; //协议列表

}

相关文章

网友评论

      本文标题:runtime

      本文链接:https://www.haomeiwen.com/subject/piqljttx.html