iOS关联对象示例详解
程序员文章站
2023-11-18 16:49:16
背景
在ios开发中如果我们想给一个对象动态添加属性或者给category添加属性的时候,都是通过runtime的关联对象去实现,那我们添加的属性到底是如何存取的呢?...
背景
在ios开发中如果我们想给一个对象动态添加属性或者给category添加属性的时候,都是通过runtime的关联对象去实现,那我们添加的属性到底是如何存取的呢?是直接添加到了对象自身的内存中了去吗?带着这些疑问让我们看一runtime的源码,解开关联对象的神秘面纱。
关联对象源码
存值
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 | 对应的key |
id 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; }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。
下一篇: C# WinForm窗口最小化到系统托盘