欢迎光临
我们一直在努力

iOS底层原理 – 分类category的本质以及源码分析

开篇之前大家先思考这两个问题

Category的实现原理?
Category和Extension的区别是什么?

Class Extension在编译的时候,它的数据就已经包含在类信息中
Category是在运行时,才会将数据合并到类信息中

开始分析Category的源码

  • 1.创建个LSPerson类
@interface LSPerson : NSObject
@end
@implementation LSPerson
@end
  • 2.创建个LSPerson的Test分类
@interface LSPerson (Test)<NSCopying,NSCoding>
@property (nonatomic,copy)NSString *name1;
@property (nonatomic,assign)int age;
@end
@implementation LSPerson (Test)
-(void)test1
{
    NSLog(@"LSPerson (Test1)");
}
-(void)test2
{
    NSLog(@"LSPerson (Test2)");
}
+(void)test3
{
    NSLog(@"LSPerson (Test3)");
}
-(id)copy
{
    return [[LSPerson alloc]init];
}
@end
  • 从上面的文件中可以看到这个分类含有2个属性,2个方法,遵循了两个协议
  • 那么现在我们用clang生成cpp代码看一下文件里都有啥
  • clang命令如下
    xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc LSPerson+Test.m
struct _category_t {
    const char *name;  //哪个类的分类 LSPerson
    struct _class_t *cls; //这个值没用到传的为0
    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;//属性列表
};
  • 经过仔细查看,看到了cpp文件里有这么个结构体名字写的也很清楚,分类的结构体,存放分类的信息,那么在接着看在哪用到这个结构体了,又看到了下面代码
static struct _category_t _OBJC_$_CATEGORY_LSPerson_$_Test __attribute__ ((used, section ("__DATA,__objc_const"))) = 
{
    "LSPerson",
    0, // &OBJC_CLASS_$_LSPerson,
    (const struct _method_list_t *)&_OBJC_$_CATEGORY_INSTANCE_METHODS_LSPerson_$_Test,
    (const struct _method_list_t *)&_OBJC_$_CATEGORY_CLASS_METHODS_LSPerson_$_Test,
    (const struct _protocol_list_t *)&_OBJC_CATEGORY_PROTOCOLS_$_LSPerson_$_Test,
    (const struct _prop_list_t *)&_OBJC_$_PROP_LIST_LSPerson_$_Test,
};
  • 看上面这段代码是定义了一个_category_t类型的变量,变量名称就是_OBJC_$_CATEGORY_类名_$_分类名,这种格式变量就不会重复,然后进行赋值,有哪几个参数那个结构体看的也很清楚了,包含类名,对象方法列表,类方法列表,协议列表,属性列表

类名就是LSPerson也都明白,那么接着看_OBJC_$_CATEGORY_INSTANCE_METHODS_LSPerson_$_Test这个变量,可以看到是取这个变量地址然后转成这个类型(const struct _method_list_t *),然后搜索这个变量名字,发现如下代码

static struct /*_method_list_t*/ {
    unsigned int entsize;  // sizeof(struct _objc_method)
    unsigned int method_count;
    struct _objc_method method_list[2];
} _OBJC_$_CATEGORY_INSTANCE_METHODS_LSPerson_$_Test __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_objc_method),
    2,
    {{(struct objc_selector *)"test1", "v16@0:8", (void *)_I_LSPerson_Test_test1},
    {(struct objc_selector *)"test2", "v16@0:8", (void *)_I_LSPerson_Test_test2}}
};

可以看出里面含有我们定义的两个对象方法,并且方法count=2
– 我们从上面看到了_objc_method这个结构体我们在搜索它的结构,发现就是方法名字,方法类型,方法实现IMP

struct _objc_method {
    struct objc_selector * _cmd;
    const char *method_type;
    void  *_imp;
};

接着看类方法,搜索那个类方法变量,又看到如下代码

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_LSPerson_$_Test __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_objc_method),
    1,
    {{(struct objc_selector *)"test3", "v16@0:8", (void *)_C_LSPerson_Test_test3}}
};

可以看出里面含有我们定义的一个类方法,并且方法count=1

然后在看协议列表,发现如下代码
static struct /*_protocol_list_t*/ {
    long protocol_count;  // Note, this is 32/64 bit
    struct _protocol_t *super_protocols[2];
} _OBJC_CATEGORY_PROTOCOLS_$_LSPerson_$_Test __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    2,
    &_OBJC_PROTOCOL_NSCopying,
    &_OBJC_PROTOCOL_NSCoding
};

可以看到包含着我们遵循的两个协议NSCopying,NSCoding,但是看起来是两个变量,再接着看这俩变量是啥

struct _protocol_t _OBJC_PROTOCOL_NSCopying __attribute__ ((used)) = {
    0,
    "NSCopying",
    0,
    (const struct method_list_t *)&_OBJC_PROTOCOL_INSTANCE_METHODS_NSCopying,
    0,
    0,
    0,
    0,
    sizeof(_protocol_t),
    0,
    (const char **)&_OBJC_PROTOCOL_METHOD_TYPES_NSCopying
};

看到我们传了协议名称,还有方法列表变量,在看方法列表变量是啥

static struct /*_method_list_t*/ {
    unsigned int entsize;  // sizeof(struct _objc_method)
    unsigned int method_count;
    struct _objc_method method_list[1];
} _OBJC_PROTOCOL_INSTANCE_METHODS_NSCopying __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_objc_method),
    1,
    {{(struct objc_selector *)"copyWithZone:", "@24@0:8^{_NSZone=}16", 0}}
};

由上面看到了这个方法列表里有我们实现的copy方法,底层调用的是copyWithZone,和alloc类似调用的是allocWithZone

那么接下来在看看协议的结构体如下,看起来也是一目了然

struct _protocol_t {
    void * isa;  // NULL
    const char *protocol_name;
    const struct _protocol_list_t * protocol_list; // super protocols
    const struct method_list_t *instance_methods;
    const struct method_list_t *class_methods;
    const struct method_list_t *optionalInstanceMethods;
    const struct method_list_t *optionalClassMethods;
    const struct _prop_list_t * properties;
    const unsigned int size;  // sizeof(struct _protocol_t)
    const unsigned int flags;  // = 0
    const char ** extendedMethodTypes;
};

接下来看属性列表源码

static struct /*_prop_list_t*/ {
    unsigned int entsize;  // sizeof(struct _prop_t)
    unsigned int count_of_properties;
    struct _prop_t prop_list[2];
} _OBJC_$_PROP_LIST_LSPerson_$_Test __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_prop_t),
    2,
    {{"name1","T@\"NSString\",C,N"},
    {"age","Ti,N"}}
};

确实看到了我们定义的两个name,age属性

由此我们得到结论就是

分类在编译的时候将分类的信息存在struct _category_t中,那么怎么在程序运行的时候是怎么加载到内存中的呢,接下来看runtime的源码
屏幕快照 2018-11-21 上午11.33.26.png

  • 我们看到attachCategories如下源码
static void 
attachCategories(Class cls, category_list *cats, bool flush_caches)
{
    if (!cats) return;
    if (PrintReplacedMethods) printReplacements(cls, cats);

    bool isMeta = cls->isMetaClass();

    // fixme rearrange to remove these intermediate allocations
    method_list_t **mlists = (method_list_t **)
        malloc(cats->count * sizeof(*mlists));
    property_list_t **proplists = (property_list_t **)
        malloc(cats->count * sizeof(*proplists));
    protocol_list_t **protolists = (protocol_list_t **)
        malloc(cats->count * sizeof(*protolists));

    // Count backwards through cats to get newest categories first
    int mcount = 0;
    int propcount = 0;
    int protocount = 0;
    int i = cats->count;
    bool fromBundle = NO;
    while (i--) {
        auto& entry = cats->list[i];

        method_list_t *mlist = entry.cat->methodsForMeta(isMeta);
        if (mlist) {
            mlists[mcount++] = mlist;
            fromBundle |= entry.hi->isBundle();
        }

        property_list_t *proplist = 
            entry.cat->propertiesForMeta(isMeta, entry.hi);
        if (proplist) {
            proplists[propcount++] = proplist;
        }

        protocol_list_t *protolist = entry.cat->protocols;
        if (protolist) {
            protolists[protocount++] = protolist;
        }
    }

    auto rw = cls->data();

    prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
    rw->methods.attachLists(mlists, mcount);
    free(mlists);
    if (flush_caches  &&  mcount > 0) flushCaches(cls);

    rw->properties.attachLists(proplists, propcount);
    free(proplists);

    rw->protocols.attachLists(protolists, protocount);
    free(protolists);
}

1542772124422.jpg

E70CD585-A902-4604-AAA1-079FC44C8C1E.png

BEA8A89A-5082-4AE8-A001-C1E122EC2A78.png

由此我们得到以下结论,Category的加载处理过程

1.通过Runtime加载某个类的所有Category数据
2.把所有Category的方法、属性、协议数据,合并到一个大数组中
后面参与编译的Category数据,会在数组的前面
3.将合并后的分类数据(方法、属性、协议),插入到类原来数据的前面

屏幕快照 2018-11-21 下午1.35.32.png

上图是Objc类对象,元类对象的底层结构,而我们分类添加的方法是添加到class_rw_t的方法列表里,从上面分析的代码可以看到访问的是

auto rw = cls->data();
prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
rw->methods.attachLists(mlists, mcount);
所以calss_ro_t的baseMethodList不会改变

接下来验证同时用分类,和类扩展添加属性,然后在获取属性列表看看顺序是啥样

  • 首先在LSPerson类扩展里添加两个属性,分类里的name,age属性不变
@interface LSPerson : NSObject
@property (nonatomic,copy)NSString *name1;
@property (nonatomic,copy)NSString *name2;
@end
@interface LSPerson()
@property (nonatomic,copy)NSString *extensionPro1;
@property (nonatomic,copy)NSString *extensionPro2;
@end
@implementation LSPerson

@end

@interface LSPerson (Test)<NSCopying,NSCoding>
@property (nonatomic,copy)NSString *categoryName1;
@property (nonatomic,copy)NSString *categoryName2;
@end
@implementation LSPerson (Test)
@end
  • 打印属性列表,使用此库比较方便 DLIntrospection
  • 可以看到打印一下结果证明
    > 先是类本身的东西
    然后编译的时候把类扩展的东西插在原来的前面
    编译的时候同时把分类的信息存放在category_t结构体里
    程序运行的时候利用runtime把分类的信息继续插在最前面
    所以存放顺序应该是:
    分类,类扩展,本类信息
(
    "@property (nonatomic, copy) NSString* categoryName1",
    "@property (nonatomic, copy) NSString* categoryName2",
    "@property (nonatomic, copy) NSString* extensionPro1",
    "@property (nonatomic, copy) NSString* extensionPro2",
    "@property (nonatomic, copy) NSString* name1",
    "@property (nonatomic, copy) NSString* name2"
)

赞(5) 打赏
未经允许不得转载:松哥博客 » iOS底层原理 – 分类category的本质以及源码分析
分享到: 更多 (0)

评论 19

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    Pharmacokinetics For Amoxicillin Buy Zithromax At Walgreens cialis tablets for sale Levitra Dopo Mangiato Acticin 30gm Medicine

    Randpycle3个月前 (06-04)回复
  2. #2

    Kamagra 100mg Test Cialis Generico Prezzi buy cialis online Priligy Yahoo

    Randpycle2个月前 (06-17)回复
  3. #3

    Levitra En Generique Ed Sample Pack Canadain Phamacry viagra Gimonte Kamagra Prescription Drugs Viagra

    Randpycle2个月前 (06-29)回复
  4. #4

    L Thyroxine Zithromax Weekly canadian cialis Purchase Tamoxifen Online Costo Cialis 2 5 Mg Cialis Sotto La Lingua

    Randpycle1个月前 (07-12)回复
  5. #5

    Viagra Einzelne Pillen Kaufen cialis no prescription Comprar Levitra Online Prezzo Ufficiale Levitra Bayer

    Randpycle1个月前 (07-20)回复
  6. #6

    Hi where i can buy
    http://amoxicillin500mg.club – amoxicillin 500 milligrams ? Please help me found
    amoxicillin 500 dosage
    Thanks!

    Jerrcyseste4周前 (07-25)回复
  7. #7

    Hi where i can buy
    http://amoxicillin500mg.club – amoxicillin 500 mg capsule ? Please help me found
    amoxicillin 500 mg capsule
    Thanks!

    Jerrcyseste4周前 (07-25)回复
  8. #8
  9. #9

    3d15 forum achat cialis internet

    [url=http://cialisyqwoi.com/]cialis generico en juare[/url]
    genuine cialis price

    purchase cialis on line

    GalyaRuite2周前 (08-08)回复
  10. #10

    3d15 forum achat cialis internet

    [url=http://cialisyqwoi.com/]generic cialis[/url]
    paypal para comprar cialis

    cialis e sessualita

    GalyaRuite2周前 (08-08)回复
  11. #11

    I was having a the whole kit of pain, and I was not indubitable what to do. I gave it a some days until I went to see my doctor. So I went to picture him, and he only told me to get on the other side of it. I was so thankful for that reaction because I realized that the official medical community did not suffering about me more than I care here myself. Therefore, I did the investigate and I found the mend to all of my desire here: [url=https://buycbdoilcure.com/]buycbdoilcure.com[/url]

    PlokerGaw2周前 (08-08)回复
  12. #12

    3d15 online generico di cialis

    [url=http://cialisyqwoi.com/]cialisis medicamentos[/url]
    cialis niederlande

    generische cialis kanada

    GalyaRuite2周前 (08-08)回复
  13. #13

    how do i get viagra prescribed

    RannevRuite2周前 (08-12)回复
  14. #14

    3d15 cialis dosages levels

    [url=http://cialisyqwoi.com/]cialis dosages levels[/url]
    kpa cialis apoteket

    visit our site sample cialis

    GalyaRuite1周前 (08-13)回复
  15. #15

    3d15 cialis precio espaa

    [url=http://cialisyqwoi.com/]cialis dose for 80 year old[/url]
    cialis generic sales

    consiglio cialis generico

    GalyaRuite1周前 (08-13)回复
  16. #16

    3d15 cialis bestellen rezeptfrei

    [url=http://cialisyqwoi.com/]order generic cialis onli[/url]
    acquisto cialis in giappone

    cialis generico 5 mg prezz

    GalyaRuite1周前 (08-13)回复
  17. #17

    be is at walmart can

    4a57 is fakten

    GermanRuite1周前 (08-14)回复
  18. #18

    f8b3 buy female viagra online india

    [url=http://viagraqwoiu.com/]comprare viagra austria[/url]
    kpa viagra i spanien

    comprar viagra en europ

    GermanRuite1周前 (08-15)回复
  19. #19

    I was having on the spot figuring wrong what I should to do. I tried so scads other things in preference to I came to a terminal conclusion. My ally and I were talking a given hour, and it bash me out-moded of nowhere. I speedily realized that I heard of a website some spell ago that would facilitate me out. I came on here because I wanted to piece my mind-blowing invention with you all. Here it is: [url=https://mypersonalloansonline.com/]mypersonalloansonline.com/[/url]

    sokzyqmat2天前回复

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏