iOS - 知识梳理 (杂集)

因为有些知识点比较小,不想浪费篇幅单独弄一篇博客,所以把一些小的知识点记录在一篇里面,方便查阅。

1.Category 会覆盖掉原来类里的方法吗?

答案是会的,将category生成对应的cpp文件会看到它的结构,

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;
};

 实例方法列表,类方法列表,协议列表,属性列表,但是没有实例变量列表。即使添加了属性也只是添加了setter和 getter方法。

系统在运行时将category对应的实例方法、类方法等都插入到了原来类或元类的方法列表中去了,而且是放在前边,所以通过isa指针去查找方法的话,优先是调用category中的。所以是category会覆盖掉原来类实现的方法。

猜你喜欢

转载自blog.csdn.net/evol_f/article/details/82965081