关联对象原理
关联对象并不是存储在被关联对象本身内存中,关联对象存储在全局的统一的一个AssociationsManager中
-
获取到管理所有关联对象的hashmap总表的管理者
AssociationsManager
,然后拿到hashmap总表AssociationsHashMap
-
DISGUISE(object
对关联对象的地址进行取反操作得到哈希表对应的下标index
-
如果
new_value
为空(即对属性赋值为nil)就直接找到相应的表进行删除 -
如果
new_value
不为空,就拿到总表的迭代器通过拿到的下标index进行遍历查找;- 如果AssociationsHashMap从没有对象的关联信息表,那么就创建一个map并通过传入的key把value存进去.
- 如果找到管理对象的关联属性哈希map表,然后再通过key去遍历取值
如果取到了,就先把新值设置到key上,再将旧值释放掉
如果没取到,就直接将新值设置在key上
setter方法
void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy) {
// retain the new value (if any) outside the lock.
ObjcAssociation old_association(0, nil);
//acquireValue会对retain和copy进行操作
id new_value = value ? acquireValue(value, policy) : nil;
{
// 关联对象的管理类
AssociationsManager manager;
// 获取关联的 HashMap -> 存储当前关联对象
AssociationsHashMap &associations(manager.associations());
// 对当前的对象的地址做按位去反操作 - 就是 HashMap 的key (哈希函数)
disguised_ptr_t disguised_object = DISGUISE(object);
if (new_value) {
// break any existing association.
// 获取 AssociationsHashMap 的迭代器 - (对象的) 进行遍历
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
// secondary table exists
ObjectAssociationMap *refs = i->second;
// 根据key去获取关联属性的迭代器
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
old_association = j->second;
// 替换设置新值
j->second = ObjcAssociation(policy, new_value);
} else {
// 到最后了 - 直接设置新值
(*refs)[key] = ObjcAssociation(policy, new_value);
}
} else {
/*
如果AssociationsHashMap从没有对象的关联信息表,
那么就创建一个map并通过传入的key把value存进去.
*/
ObjectAssociationMap *refs = new ObjectAssociationMap;
associations[disguised_object] = refs;
(*refs)[key] = ObjcAssociation(policy, new_value);
object->setHasAssociatedObjects();
}
} else {
/*
如果传入的value是nil,并且之前使用相同的key存储过关联对象,
那么就把这个关联的value移除(这也是为什么传入nil对象能够把对象的关联value移除)
*/
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
old_association = j->second;
refs->erase(j);
}
}
}
}
// release the old value (outside of the lock).
//最后把之前使用传入的这个key存储的关联的value释放(OBJC_ASSOCIATION_SETTER_RETAIN策略存储的)
if (old_association.hasValue()) ReleaseValue()(old_association);
}
getter方法
id _object_get_associative_reference(id object, void *key) {
id value = nil;
uintptr_t policy = OBJC_ASSOCIATION_ASSIGN;
{
// 关联对象的管理类
AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());
// 生成伪装地址。处理参数 object 地址
disguised_ptr_t disguised_object = DISGUISE(object);
// 所有对象的额迭代器
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
ObjectAssociationMap *refs = i->second;
// 内部对象的迭代器
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
// 找到 - 把值和策略读取出来
ObjcAssociation &entry = j->second;
value = entry.value();
policy = entry.policy();
// OBJC_ASSOCIATION_GETTER_RETAIN - 就会持有一下
if (policy & OBJC_ASSOCIATION_GETTER_RETAIN) {
objc_retain(value);
}
}
}
}
if (value && (policy & OBJC_ASSOCIATION_GETTER_AUTORELEASE)) {
objc_autorelease(value);
}
return value;
}
关联对象技术核心对象及关系如下
/*
AssociationsManager;
AssociationsHashMap;
ObjectAssociationMap;
ObjectAssociationMap;
*/
class AssociationsManager {
static AssociationsHashMap *_map;
}
class AssociationsHashMap : public unordered_map<disguised_ptr_t, ObjectAssociationMap *, DisguisedPointerHash, DisguisedPointerEqual, AssociationsHashMapAllocator>
class ObjectAssociationMap : public std::map<void *, ObjcAssociation, ObjectPointerLess, ObjectAssociationMapAllocator>
class ObjcAssociation {
uintptr_t _policy;
id _value;
}
关联对象原理2.png
关联对象操作
//添加关联对象
objc_setAssociatedObject(id _Nonnull object,
const void * _Nonnull key,
id _Nullable value,
objc_AssociationPolicy policy)
}
/*
1.AssociationsManager里面有个变量AssociationsHashMap
2.AssociationsHashMap的key是object(即self),value是ObjectAssociationMap对象
3.ObjectAssociationMap的key是上面方法中传入的key,value是ObjcAssociation
4.ObjcAssociation里面有两个属性 policy和value,这两个就是我们上面方法传入的值
*/
// 获取关联对象
id objc_getAssociatedObject(id object, const void * key)
// 移除所有的关联对象
void objc_removeAssociatedObjects(id object)
Key的常见用法
_cmd: 代表当前方法的@selector(selector),每一个方法都带有两个隐式参数self 和 _cmd
-
使用指针
static const void *MJNameKey = &MJNameKey; - (void)setName:(NSString *)name { objc_setAssociatedObject(self, MJNameKey, name, OBJC_ASSOCIATION_COPY_NONATOMIC); } - (NSString *)name { return objc_getAssociatedObject(self, MJNameKey); }
-
使用char
static const char MJNameKey; - (void)setName:(NSString *)name { objc_setAssociatedObject(self, &MJNameKey, name, OBJC_ASSOCIATION_COPY_NONATOMIC); } - (NSString *)name { return objc_getAssociatedObject(self, &MJNameKey); }
-
使用属性名作为key
#define MJNameKey @"name" - (void)setName:(NSString *)name { objc_setAssociatedObject(self, MJNameKey, name, OBJC_ASSOCIATION_COPY_NONATOMIC); } - (NSString *)name { return objc_getAssociatedObject(self, MJNameKey); }
-
使用get方法的@selecor作为key
- (void)setName:(NSString *)name { objc_setAssociatedObject(self, @selector(name), name, OBJC_ASSOCIATION_COPY_NONATOMIC); } - (NSString *)name { // 隐式参数: _cmd == @selector(name) return objc_getAssociatedObject(self, _cmd); }
网友评论