iOS 底层原理

Category分类

2020-12-28  本文已影响0人  天空像天空一样蓝

一、Demo展示

创建一个Person类,在创建一个Person+eatPerson+test两个分类。

@implementation Person
- (void)run {
    NSLog(@"run");
}
@end

@implementation Person (test)
- (void)test {
    NSLog(@"test");
}
@end

@implementation Person (eat)
- (void)eat {
    NSLog(@"eat");
}
@end

// 运行下面代码
Person *person = [[Person alloc] init];

[person run];
[person test];
[person eat];
        

当然上面代码,会打印出”run“/"test"/"eat"
我们知道当我们调用一个方法是,底层会调用objc_msgSend(person, @selector(xxx))这个方法,根据OC对象的本质得知,具体的实现是person 的isa 找到类对象里面的实例方法,如果是类方法,则会去元类对象找类方法。

思考如上 Demo里面的两个分类会生成两个新的类吗?
不会,一个isa只会有一个类对象,程序会通过runtime动态将实例方法合并到类对象里面的对象方法中,类方法都会合并到元类对象的类方法

二、Category 内部实现

(一)demo1

使用clang编译器把OC代码转成C++,在终端上cd到当前项目的目录,输入xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc Person+eat.m会自动生成 .cpp 文件

打开上面生成的Person+eat.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; // 属性属性列表
};

// 生成的 Category
static struct _category_t _OBJC_$_CATEGORY_Person_$_eat __attribute__ ((used, section ("__DATA,__objc_const"))) = 
{
    "Person", // 给了我们上面的name
    0, // &OBJC_CLASS_$_Person,
    (const struct _method_list_t *)&_OBJC_$_CATEGORY_INSTANCE_METHODS_Person_$_eat, // 就是我们的实例方法 instance_methods
    0,
    0,
    0,
};

// _OBJC_$_CATEGORY_INSTANCE_METHODS_Person_$_eat
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_Person_$_eat __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_objc_method),
    1,
    {{(struct objc_selector *)"eat", "v16@0:8", (void *)_I_Person_eat_eat}}
};

(二)demo2

Person+test.h类中添加

@property (assign, nonatomic) int weight;
@property (assign, nonatomic) double height;

Person+test.m类中添加

+ (void)test {
    NSLog(@"+test");
}
- (void)test1
{
    NSLog(@"eat1");
}

+ (void)test2
{
    
}

+ (void)test3
{
    
}

然后同样运行上面 clang 代码xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc Person+test.m 生成.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;
};

// 
static struct _category_t _OBJC_$_CATEGORY_Person_$_test __attribute__ ((used, section ("__DATA,__objc_const"))) = 
{
    "Person",
    0, // &OBJC_CLASS_$_Person,
    (const struct _method_list_t *)&_OBJC_$_CATEGORY_INSTANCE_METHODS_Person_$_test,
    (const struct _method_list_t *)&_OBJC_$_CATEGORY_CLASS_METHODS_Person_$_test,
    0,
    (const struct _prop_list_t *)&_OBJC_$_PROP_LIST_Person_$_test,
};

// 实例,_OBJC_$_CATEGORY_INSTANCE_METHODS_Person_$_test
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_Person_$_test __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_objc_method),
    2,
    {{(struct objc_selector *)"test", "v16@0:8", (void *)_I_Person_test_test},
    {(struct objc_selector *)"test1", "v16@0:8", (void *)_I_Person_test_test1}}
};
// 类方法 _OBJC_$_CATEGORY_CLASS_METHODS_Person_$_test
static struct /*_method_list_t*/ {
    unsigned int entsize;  // sizeof(struct _objc_method)
    unsigned int method_count;
    struct _objc_method method_list[3];
} _OBJC_$_CATEGORY_CLASS_METHODS_Person_$_test __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_objc_method),
    3,
    {{(struct objc_selector *)"test", "v16@0:8", (void *)_C_Person_test_test},
    {(struct objc_selector *)"test2", "v16@0:8", (void *)_C_Person_test_test2},
    {(struct objc_selector *)"test3", "v16@0:8", (void *)_C_Person_test_test3}}
};
// 属性列表 _OBJC_$_PROP_LIST_Person_$_test
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_Person_$_test __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_prop_t),
    2,
    {{"weight","Ti,N"},
    {"height","Td,N"}}
};

也就是说,我们写的分类都会变成_category_t这种结构体,在合适的时机合并到类的对象方法或元类的类方法中。

(三)Category 的加载处理过程

  1. 通过 Runtime 加载某个类的所有Category 数据

2.把所有 Category 的方法、属性、协议数据、合并到一个大数组中

3.将合并后的分类数据(方法、属性、协议),插入到类原来数据的前面

所以,我们在开发过程中,如果分类和类中的方法名字相同,会调用分类里面的。

三、load函数在Category 中的加载

(一)demo

// Person
@implementation Person
+ (void)run {
    NSLog(@"Person +run");
}
+ (void)load {
    NSLog(@"Person +load");
}
@end

// Person+test
@implementation Person (test)

+ (void)load {
    NSLog(@"Person (test) +load");
}
+ (void)test {
    NSLog(@"Person (test) +test");
}
@end

// Person+eat
@implementation Person (eat)
+ (void)load {
    NSLog(@"Person (eat) +load");
}
+ (void)eat {
    NSLog(@"Person (eat) +eat");
}
@end

// 调用 Person 的 +run方法
[Person run];

我们运行程序发现

2020-06-11 23:18:48.786224+0800 TestDemo[18522:2035515] Person +load
2020-06-11 23:18:48.786723+0800 TestDemo[18522:2035515] Person (test) +load
2020-06-11 23:18:48.786785+0800 TestDemo[18522:2035515] Person (eat) +load
2020-06-11 23:18:48.786926+0800 TestDemo[18522:2035515] Person (eat) +run

思考在上面的 Category 内部实现证明了, 如果该分类的方法和该类的方法名一样,会优先调用分类的方法,类里面的方法不会被调用。为什么 load 里面的方法都会被调用呢,而不是像 run 方法一样?

(二)load 调用顺序

1、先调用的 +load 方法

2、在调用分类的 +load 方法

扩展

打印出某个类中的所有方法

- (void)printMethodNamesOfClass:(Class )cls {
    
    unsigned int count;
    // 获取方法数组
    Method *methodList = class_copyMethodList(cls, &count);
    // 存储方法名
    NSMutableString *methodNames = [NSMutableString string];
    
    // 遍历所有的方法
    for (int i = 0; i < count; i++) {
        // 获得方法
        Method method = methodList[i];
        
        // 获得方法名
        NSString *methodName = NSStringFromSelector(method_getName(method));
        // 拼接方法名
        [methodNames appendString:methodName];
        [methodNames appendString:@", "];
    }
    // 释放
    free(methodList);
    
    // 打印方法名
    NSLog(@"%@, %@", cls, methodNames);
}

问题

1、给一个存在的类添加两个分类,会生成两个新的类吗?

不会,一个isa只会有一个类对象,程序会通过runtime动态将实例方法合并到类对象里面的对象方法中,类方法都会合并到元类对象的类方法中。

2、Category 的使用场合

正式协议是通过 protocol 指定的一系列方法的声明,然后由遵守该协议的类自己去实现这些方法。而非正式协议是通过给 NSObject 或其子类添加一个分类来实现。非正式协议已经渐渐被正式协议取代,正式协议最大的优点就是可以使用泛型约束,而非正式协议不可以。)

3、Category中都可以添加哪些内容

4、Category的优缺点、特点、注意点

Category 描述
优点 1、使用场合,
2、可以按照需求加载不同的类。
缺点 1、不能直接添加成员变量,可以通过关联对象实现这种效果,
2、分类方法会“覆盖”同名的宿主类方法,如果使用不当会造成问题
特点 1、运行时决议,
2、可以有声明、可以有实现。
3、可以为系统的类添加分类,
运行时决议:Category 编译之后的底层结构是struct category_t,里面存储着分类的对象方法、类方法、属性、协议信息,这时候分类中的数据还没有合并到类中,而是在程序运行的时候通过Runtime机制将所有分类数据合并到类(类对象、元类对象)中去。(这是分类最大的特点,也是分类和扩展的最大区别,扩展是在编译的时候就将所有数据都合并到类中去了)
注意点 1、分类方法会“覆盖”同名的宿主类方法,如果使用不当会造成问题;
2、同名分类方法谁能生效取决于编译顺序,最后参与编译的分类中的同名方法会最终生效;
3、名字相同的分类会引起编译报错。

5、Category 的实现原理

6、Category的加载处理过程

在编译时,Category 中的数据还没有合并到类中,而是在程序运行的时候通过Runtime机制将所有分类数据合并到类(类对象、元类对象)中去。下面我们来看一下 Category 的加载处理过程。

① 通过Runtime加载某个类的所有 Category 数据;
② 把所有的分类数据(方法、属性、协议),合并到一个大数组中;(后面参与编译的 Category 数据,会在数组的前面)
③ 将合并后的分类数据(方法、属性、协议),插入到宿主类原来数据的前面。(所以会优先调用最后参与编译的分类中的同名方法)

上一篇下一篇

猜你喜欢

热点阅读