Runtime.h
runtime开篇定义了四个类型
typedef struct objc_method *Method;
typedef struct objc_ivar *Ivar;
typedef struct objc_category *Category;
typedef struct objc_property *objc_property_t;
上述四行代码分别 定义Method 为指向objc_method结构体的指针,Ivar为指向objc_ivar结构体的指针,Category为指向objc_category结构体的指针。objc_property_t是一个指向objc_property结构体的指针。
所谓的属性是带有set和get方法的成员变量。
struct objc_method {
SEL _Nonnull method_name OBJC2_UNAVAILABLE; //方法名,或者叫方法选择器
char * _Nullable method_types OBJC2_UNAVAILABLE; //方法类型
IMP _Nonnull method_imp OBJC2_UNAVAILABLE; //方法实现
} OBJC2_UNAVAILABLE;
struct objc_ivar {
char * _Nullable ivar_name OBJC2_UNAVAILABLE;//变量名
char * _Nullable ivar_type OBJC2_UNAVAILABLE;//变量类型
int ivar_offset OBJC2_UNAVAILABLE; //地址偏移量
#ifdef __LP64__
int space OBJC2_UNAVAILABLE; //所占内存大小
#endif
} OBJC2_UNAVAILABLE;
struct objc_category {
char * _Nonnull category_name OBJC2_UNAVAILABLE; //分类名
char * _Nonnull class_name OBJC2_UNAVAILABLE; //类名
struct objc_method_list * _Nullable instance_methods OBJC2_UNAVAILABLE; //实例方法列表
struct objc_method_list * _Nullable class_methods OBJC2_UNAVAILABLE;//类方法列表
struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;//协议列表
} OBJC2_UNAVAILABLE;
有关objc_property结构体的的信息runtime中没有找到其定义,但是找到了一个结构体,OC中用这个结构体描述了属性
typedef struct {
const char * _Nonnull name; /**< The name of the attribute */ 属性名
const char * _Nonnull value; /**< The value of the attribute (usually empty) */属性值
} objc_property_attribute_t;
struct objc_class {
Class _Nonnull isa OBJC_ISA_AVAILABILITY; //isa指针
#if !__OBJC2__
Class _Nullable super_class OBJC2_UNAVAILABLE;//指向父类
const char * _Nonnull name OBJC2_UNAVAILABLE; //类名
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;//实例大小
struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE;//成员变量列表
struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE;//方法列表
struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE;//缓存
struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;//协议列表
#endif
} OBJC2_UNAVAILABLE;
类定义了isa指针,指向自己的元类(元类也是对象,元类的isa指针指向根元类,根元类的isa指针指向它自己),如果元类不存在指向自己(根元类)。
isa指针是指向自己所属类的指针也可以从以下方法class方法的调用中得到证明
+ (Class)class {
return self;
}
- (Class)class {
return object_getClass(self);
}
Class object_getClass(id obj)
{
if (obj) return obj->getIsa();
else return Nil;
}
objc_object::getIsa()
{
if (!isTaggedPointer()) return ISA();
uintptr_t ptr = (uintptr_t)this;
if (isExtTaggedPointer()) {
uintptr_t slot =
(ptr >> _OBJC_TAG_EXT_SLOT_SHIFT) & _OBJC_TAG_EXT_SLOT_MASK;
return objc_tag_ext_classes[slot];
} else {
uintptr_t slot =
(ptr >> _OBJC_TAG_SLOT_SHIFT) & _OBJC_TAG_SLOT_MASK;
return objc_tag_classes[slot];
}
}
/// An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;
/// Represents an instance of a class.
struct objc_object {
Class _Nonnull isa OBJC_ISA_AVAILABILITY;
};
/// A pointer to an instance of a class.
typedef struct obj
c_object *id;
以上三个结构体定义在objc.h中。
Class是一个指向objc_class的结构体指针,objc_object代表的是实例对象,也是一个结构体,结构体中包含一个指向类结构体的指针,而id类型是一个指向实例对象的结构体指针,objc_object结构体中定义的Class isa 相当于 objc_class *isa,所以就有了OC中id类型可以指代任何对象。
方法描述定义
struct objc_method_description {
SEL _Nullable name; /**< The name of the method */
char * _Nullable types; /**< The types of the method arguments */
};
这里先讲下SEL是什么,我们打开objc.h文件可以看到SEL的定义为
/// An opaque type that represents a method selector.
typedef struct objc_selector *SEL;
这是一个指向结构体objc_selector的指针,objc_selector又是什么👻呢?
我们用代码检测然后打印一下看看
网友评论