一个Objective-C的类被编译器编译后情况:
一、编译后的类是如何“关联”其一个成员变量的?
/// An opaque type that represents an instance variable.
typedefstruct objc_ivar *Ivar; 即用结构体变量Ivar来“关联”。
二、那么Ivar又是一个怎样的结构体呢?
struct objc_ivar { char *ivar_name; char *ivar_type; int ivar_offset; #ifdef __alpha__ int space; #endif }
疑问:问题ivar_offset是用来干嘛的呢?请自行思考
三、编译后的类是如何“关联”其所有成员变量的?
struct objc_ivar_list { int ivar_count; #ifdef __alpha__ int space; #endif Ivar ivar_list[1]; /* variable length structure */ };由此可知:一个方法表来管理
四、参数开源代码:
/* * Instance Variable Template */ typedef struct objc_ivar *Ivar; struct objc_ivar_list { int ivar_count; #ifdef __alpha__ int space; #endif struct objc_ivar { char *ivar_name; char *ivar_type; int ivar_offset; #ifdef __alpha__ int space; #endif } ivar_list[1]; /* variable length structure */ };
五、请手工绘制成员变量的内存布局图??