iOS关联对象示例详解

2020-01-21 07:43:47刘景俊

 

内存管理策略:


typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
 OBJC_ASSOCIATION_ASSIGN = 0,  /**< Specifies a weak reference to the associated object. */
 OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.
      * The association is not made atomically. */
 OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied.
      * The association is not made atomically. */
 OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object.
      * The association is made atomically. */
 OBJC_ASSOCIATION_COPY = 01403  /**< Specifies that the associated object is copied.
      * The association is made atomically. */
};

对于四个参数理解完了之后让我们看看它真正的实现函数_object_set_associative_reference


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);
 id new_value = value ? acquireValue(value, policy) : nil;
 {
 AssociationsManager manager;
 AssociationsHashMap &associations(manager.associations());
 disguised_ptr_t disguised_object = DISGUISE(object);//得到对象地址
 if (new_value) {
  // break any existing association.
  AssociationsHashMap::iterator i = associations.find(disguised_object);//首先通过对象的地址获取对象的hashmap
  if (i != associations.end()) {//判断是否已经存在,已经存在
  // secondary table exists
  ObjectAssociationMap *refs = i->second;//取值,对应的map
  ObjectAssociationMap::iterator j = refs->find(key);//通过key查找
  if (j != refs->end()) {//如果已经存在
   old_association = j->second;//取到原来老的值,以便后边对其释放
   j->second = ObjcAssociation(policy, new_value);//存储新的值
  } else {//不存在
   (*refs)[key] = ObjcAssociation(policy, new_value);
  }
  } else {//如果不存在,创建一个
  // create the new association (first time).
  ObjectAssociationMap *refs = new ObjectAssociationMap;
  associations[disguised_object] = refs;
  (*refs)[key] = ObjcAssociation(policy, new_value);
  object->setHasAssociatedObjects();
  }
 } else {//不存在则创建一个
  // 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);
  }
  }
 }
 }
 // release the old value (outside of the lock).
 if (old_association.hasValue()) ReleaseValue()(old_association);
}