category本质
@interface NSPerson (Work)<NSCopying,NSCoding>
- (void)work;
+ (void)work;
@property(nonatomic,assign)double salary;
@end
$ xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc NSPerson+Work.m
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;
};
static struct /*_method_list_t*/ {
unsigned int entsize; // sizeof(struct _objc_method)
unsigned int method_count;
struct _objc_method method_list[1];
} _OBJC_$_CATEGORY_INSTANCE_METHODS_NSPerson_$_Work __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_objc_method),
1,
{{(struct objc_selector *)"work", "v16@0:8", (void *)_I_NSPerson_Work_work}}
};
static struct /*_method_list_t*/ {
unsigned int entsize; // sizeof(struct _objc_method)
unsigned int method_count;
struct _objc_method method_list[1];
} _OBJC_$_CATEGORY_CLASS_METHODS_NSPerson_$_Work __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_objc_method),
1,
{{(struct objc_selector *)"work", "v16@0:8", (void *)_C_NSPerson_Work_work}}
};
static struct /*_protocol_list_t*/ {
long protocol_count; // Note, this is 32/64 bit
struct _protocol_t *super_protocols[2];
} _OBJC_CATEGORY_PROTOCOLS_$_NSPerson_$_Work __attribute__ ((used, section ("__DATA,__objc_const"))) = {
2,
&_OBJC_PROTOCOL_NSCopying,
&_OBJC_PROTOCOL_NSCoding
};
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_NSPerson_$_Work __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_prop_t),
1,
{{"salary","Td,N"}}
};
static struct _category_t _OBJC_$_CATEGORY_NSPerson_$_Work __attribute__ ((used, section ("__DATA,__objc_const"))) =
{
"NSPerson",
0, // &OBJC_CLASS_$_NSPerson,
(const struct _method_list_t *)&_OBJC_$_CATEGORY_INSTANCE_METHODS_NSPerson_$_Work,
(const struct _method_list_t *)&_OBJC_$_CATEGORY_CLASS_METHODS_NSPerson_$_Work,
(const struct _protocol_list_t *)&_OBJC_CATEGORY_PROTOCOLS_$_NSPerson_$_Work,
(const struct _prop_list_t *)&_OBJC_$_PROP_LIST_NSPerson_$_Work,
};
category attach2Class
objc-os.mm
1. objc-init
//called by map_images
3. map_images_nolock
objc-runtime-new.mm
//called by objc-init
2. map_images
//called by map_images_nolock
4. _read_images
5. load_categories_nolock
6. attachCategories
7.rw->method.attachLists
objc-runtime-new.h
class list_array_tt {
public:
void attachLists(List* const * addedLists, uint32_t addedCount) {
if (addedCount == 0) return;
if (hasArray()) {
// many lists -> many lists
uint32_t oldCount = array()->count;
uint32_t newCount = oldCount + addedCount;
setArray((array_t *)realloc(array(), array_t::byteSize(newCount)));
array()->count = newCount;
memmove(array()->lists + addedCount, array()->lists,
oldCount * sizeof(array()->lists[0]));
memcpy(array()->lists, addedLists,
addedCount * sizeof(array()->lists[0]));
}
else if (!list && addedCount == 1) {
// 0 lists -> 1 list
list = addedLists[0];
}
else {
// 1 list -> many lists
List* oldList = list;
uint32_t oldCount = oldList ? 1 : 0;
uint32_t newCount = oldCount + addedCount;
setArray((array_t *)malloc(array_t::byteSize(newCount)));
array()->count = newCount;
if (oldList) array()->lists[addedCount] = oldList;
memcpy(array()->lists, addedLists,
addedCount * sizeof(array()->lists[0]));
}
}
};
class property_array_t :
public list_array_tt{};
class protocol_array_t :
public list_array_tt{};
class method_array_t :
public list_array_tt{};
category 同名方法
- category方法与class中方法重名,会调用最后编译的category中的方法,
category与class extension区别:
- category在运行时将method, protocol,properties 合并到class前面,最后编译的category在最前面
- class extension 在编译时就进行合并,class extension中的元素只是限制了访问权限。
网友评论