iOS 类别不能添加属性原理?
关注者
17被浏览
9,799登录后你可以
不限量看优质回答私信答主深度交流精彩内容一键收藏
Category 在 runtime 中是用一个结构体表示的:
struct _category_t {
const char *name;
struct _class_t *cls;
const struct _method_list_t *instance_methods;
const struct _method_list_t *class_methods;
const struct _protocol_list_t *protocols;
const struct _prop_list_t *properties;
};
里面虽然可以添加 property,但是这些 properties 并不会自动生成 Ivar,也就是不会有 @synthesize 的作用,dyld 加载的期间,这些 categories 会被加载并 patch 到相应的类中。这个过程是一个动态过程,Ivar 不能动态添加,因为表示 ObjC 类的结构体运行时并不能改变。