1.在iOS 引用「1.NSObject--- clang编译OC成C++或者C文件」的类:BaseObj
给此类.h文件添加非原子属性:@property (nonatomic,copy) NSString *name;
此时 BaseObj_IMPL这个结构体中会自动添加_name的变量
struct BaseObj_IMPL {
struct NSObject_IMPL NSObject_IVARS;
NSString * _Nonnull _name;//变量
}
在@implementation BaseObj也发生变化了
生成底层相关的代码:
可以看出:1)、自动生成成员变量 2)、setter 3 )、get
static NSString * _Nonnull _I_BaseObj_name(BaseObj * self, SEL _cmd) { return (*(NSString * _Nonnull *)((char *)self + OBJC_IVAR_$_baseObj$_name)); }
extern "C" __declspec(dllimport) void objc_setProperty (id, SEL, long, id, bool, bool);
static void _I_BaseObj_setName_(BaseObj * self, SEL _cmd, NSString * _Nonnull name) { objc_setProperty (self, _cmd, __OFFSETOFIVAR__(struct BaseObj, _name), (id)name, 0, 1); }
2.生成一大堆结构体
{ _prop_t
_protocol_t
_objc_method
_ivar_t
_class_ro_t
_class_t
_category_t
}
3.类实例变量的生成
extern "C" unsigned long int OBJC_IVAR_$_BaseObj$_name __attribute__ ((used, section ("__DATA,__objc_ivar"))) = __OFFSETOFIVAR__(struct BaseObj, _name);
static struct /*_ivar_list_t*/ {
unsigned int entsize; // sizeof(struct _prop_t)
unsigned int count;
struct _ivar_t ivar_list[1];
} _OBJC_$_INSTANCE_VARIABLES_BaseObj __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_ivar_t),
1,
{{(unsigned long int *)&OBJC_IVAR_$_BaseObj$_name, "_name", "@\"NSString\"", 3, 8}}
};
4.类实例方法的生成
NSString :
setter:(struct objc_selector *)"setName:", "v24@0:8@16", (void *)_I_BaseObj_setName_
get :(struct objc_selector *)"name", "@16@0:8", (void *)_I_BaseObj_name
NSInteger:
setter:(struct objc_selector *)"setAge:", "v24@0:8q16", (void *)_I_BaseObj_setAge_
get :{(struct objc_selector *)"age", "q16@0:8", (void *)_I_BaseObj_age
static struct /*_method_list_t*/ {
unsigned int entsize; // sizeof(struct _objc_method)
unsigned int method_count;
struct _objc_method method_list[2];
} _OBJC_$_INSTANCE_METHODS_BaseObj __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_objc_method),
2, //NSString 所创建的 大致占有 2个字节 NSInteger:占有 2个字节
{{(struct objc_selector *)"name", "@16@0:8", (void *)_I_BaseObj_name},
{(struct objc_selector *)"setName:", "v24@0:8@16", (void *)_I_BaseObj_setName_}}
/*如果添加属性age 并有assgin修饰
{{(struct objc_selector *)"age", "q16@0:8", (void *)_I_BaseObj_age}
{(struct objc_selector *)"setAge:", "v24@0:8q16", (void *)_I_BaseObj_setAge_}}
*/
};
5.类属性的生成_prop_list_t
NSString生成属性的格式:{"name","T@\"NSString\",C,N,V_name"}
NSInteger生成属性的格式:{"age","Tq,N,V_age"}
static struct /*_prop_list_t*/ {
unsigned int entsize; // sizeof(struct _prop_t)
unsigned int count_of_properties;
struct _prop_t prop_list[1];
} _OBJC_$_PROP_LIST_BaseObj __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_prop_t),
1,
{{"name","T@\"NSString\",C,N,V_name"},{"age","Tq,N,V_age"}}
};
5.生成_class_ro_t
class_ro_t 和 class_rw_t 的区别?
ObjC 类中的属性、方法还有遵循的协议等信息都保存在 class_rw_t 中:
其中还有一个指向常量的指针 ro,其中存储了当前类在编译期就已经确定的属性、方法以及遵循的协议。
1)、_OBJC_METACLASS_RO_ 没有发生变化
static struct _class_ro_t _OBJC_METACLASS_RO_$_BaseObj __attribute__ ((used, section ("__DATA,__objc_const"))) = {
1, sizeof(struct _class_t), sizeof(struct _class_t),
(unsigned int)0,
0,
"BaseObj",
0,
0,
0,
0,
0,
};
2)、_OBJC_CLASS_RO_ 发生变化
(1)、添加实例方法 (2)、添加成员变量 (3)、添加属性列表
static struct _class_ro_t _OBJC_CLASS_RO_$_BaseObj __attribute__ ((used, section ("__DATA,__objc_const"))) = {
0, __OFFSETOFIVAR__(struct BaseObj, _name), sizeof(struct BaseObj_IMPL),
(unsigned int)0,
0,
"BaseObj",
(const struct _method_list_t *)&_OBJC_$_INSTANCE_METHODS_BaseObj,
0,
(const struct _ivar_list_t *)&_OBJC_$_INSTANCE_VARIABLES_BaseObj,
0,
(const struct _prop_list_t *)&_OBJC_$_PROP_LIST_BaseObj,
};