ObjC运行时部分概念解析(二)
上篇文章简单的说明了两个关键字究竟是什么,这里主要讲讲ObjC中各种基本内存模型
Method
typedef struct objc_method *Method;
struct objc_method {
SEL method_name OBJC2_UNAVAILABLE;
char *method_types OBJC2_UNAVAILABLE;
IMP method_imp OBJC2_UNAVAILABLE;
}
可以看到一个方法(Method)是一个指向objc_method的指针.包含了函数名,函数类型,函数地址.
函数类型应该就是函数签名,它看起来是类似于这样的"v@:" 这是代码运行时将代码和参数push进栈时确定各种类型后可以计算便宜来准确的获取参数,比如int 4个字节 指针8个字节,它们在内存中时紧密排列的,比如读到i,那么我去读4个字节作为int.读到@,去取8个字节....
下面是runtime中所有的类型
#define _C_ID '@'
#define _C_CLASS '#'
#define _C_SEL ':'
#define _C_CHR 'c'
#define _C_UCHR 'C'
#define _C_SHT 's'
#define _C_USHT 'S'
#define _C_INT 'i'
#define _C_UINT 'I'
#define _C_LNG 'l'
#define _C_ULNG 'L'
#define _C_LNG_LNG 'q'
#define _C_ULNG_LNG 'Q'
#define _C_FLT 'f'
#define _C_DBL 'd'
#define _C_BFLD 'b'
#define _C_BOOL 'B'
#define _C_VOID 'v'
#define _C_UNDEF '?'
#define _C_PTR '^'
#define _C_CHARPTR '*'
#define _C_ATOM '%'
#define _C_ARY_B '['
#define _C_ARY_E ']'
#define _C_UNION_B '('
#define _C_UNION_E ')'
#define _C_STRUCT_B '{'
#define _C_STRUCT_E '}'
#define _C_VECTOR '!'
#define _C_CONST 'r'
Ivar(实例变量)
typedef struct objc_ivar *Ivar;
struct objc_ivar {
char *ivar_name OBJC2_UNAVAILABLE;
char *ivar_type OBJC2_UNAVAILABLE;
int ivar_offset OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
}
一个实例变量包含变量名,变量类型(int,long,id...),偏移,这个是从1个对象获取该实例变量存放的位置用的.最后space目前尚不清楚