关联对象

2019-02-13  本文已影响0人  滨滨_57b5

由于分类是不能直接添加成员变量的,因此需要使用关联对象技术为分类添加成员变量,由此引出几个问题:

源码分析:objc_setAssociatedObject

入口函数

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) {
   
   _object_set_associative_reference(object, (void *)key, value, policy);//其中key做了一次强制类型转换
}

主函数

void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy) {

    //1.创建一个空的ObjcAssociation对象
    ObjcAssociation old_association(0, nil);

    // 2.对关联对象进行内存管理操作retain/copy
    id new_value = value ? acquireValue(value, policy) : nil;
    {
        //3.创建AssociationsManager对象和其成员变量AssociationsHashMap对象
        AssociationsManager manager;
        AssociationsHashMap &associations(manager.associations());
        
        //4.将被关联对象的指针地址反转
        disguised_ptr_t disguised_object = DISGUISE(object);

        // 5.判断传入的关联对象是否为空
        if (new_value) {
             //如果不为空
             /*通过被关联对象的指针地址从AssociationsHashMap中获取对应的      
              ObjectAssociationMap
            */
            AssociationsHashMap::iterator i = associations.find(disguised_object);
            //如果该被关联对象已经存在了对应的ObjectAssociationMap
            if (i != associations.end()) {
                // 通过参数key获取到ObjectAssociationMap对应的ObjcAssociation
                ObjectAssociationMap *refs = i->second;
                ObjectAssociationMap::iterator j = refs->find(key);
                //如果存在对应的ObjcAssociation
                if (j != refs->end()) {
                    //把开始创建的空的ObjcAssociation对象设置为找到的这个ObjcAssociation
                    old_association = j->second;
                    //把当前的关联对象封装为ObjcAssociation并赋值给ObjectAssociationMap中key对应的值
                    j->second = ObjcAssociation(policy, new_value);
                } else {//如果没有找到存在对应的ObjcAssociation,那就根据参数key直接设置
                    (*refs)[key] = ObjcAssociation(policy, new_value);
                }
            } else { //如果该被关联对象不存在了对应的ObjectAssociationMap
                // 那么就创建新的ObjectAssociationMap
                ObjectAssociationMap *refs = new ObjectAssociationMap;
                //以被关联对象的反转地址为key,新创建的ObjectAssociationMap为value,映射为ObjectAssociationHashMap
                associations[disguised_object] = refs;
                //把参数key作为ObjectAssociationMap的key,ObjcAssociation对象为value
                (*refs)[key] = ObjcAssociation(policy, new_value);
                //最后设置这个被关联对象为有关联对象
                object->setHasAssociatedObjects();
            }
        } else {//如果传入的关联对象为nil,会将被关联对象的对应关联对象擦除
            // setting the association to nil breaks the association.
            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);
                }
            }
        }
    }
    // 最后将old_association擦除
    if (old_association.hasValue()) ReleaseValue()(old_association);
}
关联对象本质图.png

根据传入的关联对象value和关联策略policy,调用对应的retain/copy方法

static id acquireValue(id value, uintptr_t policy) {
    switch (policy & 0xFF) {
    case OBJC_ASSOCIATION_SETTER_RETAIN:
        return objc_retain(value);
    case OBJC_ASSOCIATION_SETTER_COPY:
        return ((id(*)(id, SEL))objc_msgSend)(value, SEL_copy);
    }
    return value;
}

上一篇下一篇

猜你喜欢

热点阅读