美文网首页iOS底层原理
小码哥底层原理笔记:Catgory关联对象的原理

小码哥底层原理笔记:Catgory关联对象的原理

作者: chilim | 来源:发表于2020-05-28 10:14 被阅读0次

我们知道Catgory可以定义属性,但是不能定义成员变量。比如我们这样写:

@interface Person (Test)
{
   int _weight;
}

@property (nonatomic, assign) int weight;//这句代码只会做一件事
@end

编译一下,是会报错的,提示分类不能添加成员变量。我们只能像下面这样定义属性,
我们先创建一个Person的分类Test:

@interface Person (Test)

@property (nonatomic, copy) NSString *name;

@property (nonatomic, assign) int weight;//这句代码只会做一件事
//就是只会生成这两句声明
/*
 - (void)setWeight:(int)weight;
 - (int)weight;
 */

@end

#import "Person+Test.h"
#import <objc/runtime.h>

@implementation Person (Test)

@end

如上代码所示,我们给这个Person分类Test定义了两个属性,一个是@property (nonatomic, copy) NSString *name;,一个是@property (nonatomic, assign) int weight;
现在我们执行以下代码:

Person *person = [[Person alloc] init];
person.weight = 20;

发现闪退了,报错是提示没有找到setWeight:方法。这是因为通过@property (nonatomic, copy) NSString *name;定义的属性只是声明了一个get和set方法,但是并没有实现get,set方法。那么我们要像Person类那样正常去使用name和weight属性,我们就必须在.m文件里面实现对应的set和get方法。在set方法中保存weight的值20,然后在get方法中取出20。

那么我们之间跟之前那样写行不行呢,像这样,

@implementation Person (Test)

- (void) setWeight:(int) weight{
   _weight = weight;
}

- (int)weight{
 return _weight;
}
@end

我们运行发现这样行不通,打印weight的值是0,并不是我们赋值的20。那是因为在正常Person类里面是把值保存在成员变量里面的。现在分类是没有成员变量的,所以无法保存值20。

对此我们的主要问题就是在没有成员变量的情况下如何把赋给weight的值20保存起来,我们有以下几种方案,在.m里面定义一个全局变量保存赋给weight的值,或者在.m里面定义一个全局字典,通过key-value来保存对应的属性的赋值。还有就是通过runtime的API方法来实现,如下:

#import "Person+Test.h"
#import <objc/runtime.h>

@implementation Person (Test)

- (void)setWeight:(int)weight{
    objc_setAssociatedObject(self, @selector(weight), @(weight), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (int)weight{
    return [objc_getAssociatedObject(self, _cmd) intValue];
}

- (void)setName:(NSString *)name{//_cmd =@selector(setName:)
    objc_setAssociatedObject(self, @selector(name), name, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)name{//_cmd代表当前方法的selector,_cmd =@selector(name);,但是set方法不能这样写
    return objc_getAssociatedObject(self, _cmd);
}

//static const char MJNameKey;//定义一个只占一个字节的字符串。比下面这个方法要更好
//static const char MJWeightKey;
//
//- (void)setWeight:(int)weight{
//    objc_setAssociatedObject(self, &MJWeightKey, @(weight), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//}
//
//- (int)weight{
//    return [objc_getAssociatedObject(self, &MJWeightKey) intValue];
//}
//
//- (void)setName:(NSString *)name{
//    objc_setAssociatedObject(self, &MJNameKey, name, OBJC_ASSOCIATION_COPY_NONATOMIC);
//}
//
//- (NSString *)name{
//    return objc_getAssociatedObject(self, &MJNameKey);
//}

//static const void *MJNameKey = &MJNameKey;
//static const void *MJWeightKey = &MJWeightKey;
//
//- (void)setWeight:(int)weight{
//    objc_setAssociatedObject(self, MJWeightKey, @(weight), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//}
//
//- (int)weight{
//    return [objc_getAssociatedObject(self, MJWeightKey) intValue];
//}
//
//- (void)setName:(NSString *)name{
//    objc_setAssociatedObject(self, MJNameKey, name, OBJC_ASSOCIATION_COPY_NONATOMIC);
//}
//
//- (NSString *)name{
//    return objc_getAssociatedObject(self, MJNameKey);
//}

@end

这几种方法都可以,其中第一种是最好的。还是回到上一个问题,这样用runtime实现的话,那这个成员变量是保存在哪里呢?是合并到Person类里面吗?答案不是的。runtime为这个分类的关联对象单独进行管理,并不是合并到Person类里面了。

其实runtime是生成一个AssociationsManager类用来管理分类的关联对象,让分类能正常使用成员变量,有点类似于前面说到的用字典去保存的原理。通过查看源码,我们可以知道AssociationsManager类有一个AssociationsHashMap属性,这个属性是相当于一个字典,用来存储对象-关联对象的,也就是它的key是我们关联对象时传的self,也就是这个Person分类,以这个为key,然后value是一个ObjectAssociationMapObjectAssociationMap对象也相当于是一个字典,这个字典的key是我们关联对象时传进去的那个key,value是ObjcAssociationObjcAssociation对象里面有两个属性_value_policy,这两个就是我们关联对象的值和关联策略了。

简单说AssociationsHashMap存储的是项目中所有分类的关联对象,里面应该是长这样{Person分类: AssociationsHashMap,Student分类: AssociationsHashMap},我们项目中有几个分类有关联对象,那AssociationsHashMap里面就有多少个元素。而AssociationsHashMap里面存放的就是关联对象的keyObjcAssociation,里面应该长这样{@selector(weight):@(weight),@selector(name):name},一个分类里面有多少个关联对象AssociationsHashMap里面就有多少个元素。最后ObjcAssociation就是保存着关联对象的值和关联策略了。ObjcAssociation{unitptr_t _policy= OBJC_ASSOCIATION_RETAIN_NONATOMIC; id _value=@(weight)}。以下图很好的展示了其中的原理。

分类关联对象的原理

注意:关联策略是没有weak类型的。

对应的关联策略

面试题

1、Category能否添加成员变量?如果可以,如何给Category添加成员变量?
答:不能直接给Category添加成员变量,但是可以间接实现Category有成员变量的效果

相关文章

网友评论

    本文标题:小码哥底层原理笔记:Catgory关联对象的原理

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