美文网首页
category探究

category探究

作者: Berning | 来源:发表于2021-03-18 14:29 被阅读0次

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中的元素只是限制了访问权限。

相关文章

  • Category探究

    Category简介 category是Objective-C 2.0之后添加的语言特性,category的主要作...

  • category探究

    category本质 category attach2Class objc-os.mm objc-runtime-...

  • Category 探究

    简介 人类在进步,社会在发展,随着时间变化我们会遇到不同的问题,由此也诞生了对应的解决方法。我们编写的类也是如此,...

  • 探究Category本质

    细致的看了下Category的东西,记录一下。 Category用途:1.进行类扩展2.hook一个方法3.重写已...

  • Category相关探究

    1. Category的作用 可以减少单个文件的体积 可以把不同的功能组织到不同的Category中 可以按需加载...

  • iOS - 关联对象 (Associated Object)

    在 Category 的探究 中我们看到 Category 的底层结构为结构体,这个结构体的成员有:实例方法列表、...

  • category原理探究-2

    +load 方法解析 分析load方法前先来做一个小测验: 我们来运行一下程序,发现没有调用这些类但是load方法...

  • category原理探究-1

    category探究准备 先来创建我们测试需要的类: 以Eat分类为例,请出 clang 命令:clang -...

  • iOS - Category本质探究

    iOS - Category 1、Category简介 Category是Objective-C 2.0之后添加的...

  • iOS - Category 的探究

    Category 主要的功能是给现有的类增加新的方法,Category 的优点是: 可以“分解”庞大的逻辑,进行业...

网友评论

      本文标题:category探究

      本文链接:https://www.haomeiwen.com/subject/rmcgfktx.html