iOS中怎么关联对象

发布时间:2021-07-26 14:26:15 作者:Leah
来源:亿速云 阅读:172

这篇文章给大家介绍iOS中怎么关联对象,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

关联对象源码

存值

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) { _object_set_associative_reference(object, (void *)key, value, policy);}

我们调用此方法的时候,一共传递了四个参数:

id object需要关联的对象void *key对应的keyid value对应的值objc_AssociationPolicy policy内存管理策略

内存管理策略:

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);}

通过以上代码我们可以看出其实关联对象在存储的时候在,生成了一个AssociationsManager单例对象,所以应用中所有的管理对象都存储于此AssociationsManager中。

具体存储的实现是借助了C++的关联容器unordered_map实现的。具体可以参看代码中我加的注释。

整个过程就是通过object对象的地址存储了一个类似hashmap的东西;取到此hashmap,然后通过键值对的方式将我们需要存储的值存储到此hashmap中,这个过程中如果有旧值,则最后会将旧值就行释放

取值

取值的过程其实就比较简单了,就相当于从一个hashmap中取值

id objc_getAssociatedObject(id object, const void *key) { return _object_get_associative_reference(object, (void *)key);}

id _object_get_associative_reference(id object, void *key) { id value = nil; uintptr_t policy = OBJC_ASSOCIATION_ASSIGN; { AssociationsManager manager; AssociationsHashMap &associations(manager.associations()); 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();  if (policy & OBJC_ASSOCIATION_GETTER_RETAIN) {   objc_retain(value);  }  } } } if (value && (policy & OBJC_ASSOCIATION_GETTER_AUTORELEASE)) { objc_autorelease(value); } return value;}

关于iOS中怎么关联对象就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. iOS中EGORefreshTableHeaderView怎么用
  2. ios中UITextField怎么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios

上一篇:iOS中怎么利用UIKeyInput自定义密码输入框

下一篇:spring整合JMS如何实现同步收发消息

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》