在日常開發(fā)的過程中經(jīng)常會用到在category中添加屬性的需求,在添加屬性后是默認(rèn)不會自動生成setter和getter方法的,需要我們自己手動添加,這就用到了runtime的兩個方法,objc_setAssociatedObject和objc_getAssociatedObject。
@interface Person (category1)
@property (nonatomic, copy) NSString * address;
@end
static const NSString * associate_address = @"address";
@implementation Person (category1)
- (void)setAddress:(NSString *)address{
objc_setAssociatedObject(self,
&associate_address,
address,
OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)address{
return objc_getAssociatedObject(self, &associate_address);
}
@end
這個分類的關(guān)聯(lián)對象到底是這么實(shí)現(xiàn)的,我們在objc-runtime.mm文件中或許會找到答案
id objc_getAssociatedObject(id object, const void *key) {
return _object_get_associative_reference(object, (void *)key);
}
void objc_setAssociatedObject(id object,
const void *key,
id value,
objc_AssociationPolicy policy) {
_object_set_associative_reference(object, (void *)key, value, policy);
}
void objc_removeAssociatedObjects(id object)
{
if (object && object->hasAssociatedObjects()) {
_object_remove_assocations(object);
}
}
objc_setAssociatedObject
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方法對傳入的value進(jìn)行retain或者copy,得到新的new_value
id new_value = value ? acquireValue(value, policy) : nil;
{
// 創(chuàng)建manager,manager里面有一個AssociationsHashMap類型的哈希表
AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());
// 將object簡單處理成~uintptr_t(value)類型
disguised_ptr_t disguised_object = DISGUISE(object);
if (new_value) {
// break any existing association.
// 通過disguised_object找到對應(yīng)的AssociationsHashMap
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
// secondary table exists
// i->first disguised_object ,i->second ObjectAssociationMap
ObjectAssociationMap *refs = i->second;
// 通過key找到對應(yīng)的ObjectAssociationMap
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
// 如果對應(yīng)的ObjectAssociationMap存在,取出舊值釋放,更新新值
// j->first 綁定關(guān)聯(lián)對象傳入的key
// j->second ObjcAssociation(policy, new_value)
old_association = j->second;
j->second = ObjcAssociation(policy, new_value);
} else {
// 如果不存在,以key-ObjcAssociation鍵值對的形式存入表中
(*refs)[key] = ObjcAssociation(policy, new_value);
}
} else {
// create the new association (first time).
// 如果disguised_object對應(yīng)的哈希表不存在
ObjectAssociationMap *refs = new ObjectAssociationMap;
// 將disguised_object作為 key ,refs(ObjectAssociationMap)作為value
// 存入AssociationsHashMap表中
associations[disguised_object] = refs;
// 將傳入的key 為key,ObjcAssociation為value存入表中
(*refs)[key] = ObjcAssociation(policy, new_value);
// 設(shè)置當(dāng)前對象綁定關(guān)聯(lián)對象
object->setHasAssociatedObjects();
}
} else {
// setting the association to nil breaks the association.
// 如果設(shè)置的value 為nil,找到對應(yīng)的key刪除
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).
// 釋放 old_association
if (old_association.hasValue()) ReleaseValue()(old_association);
}
用下面的一張圖來說明AssociationsManager,AssociationsHashMap,ObjectAssociationMap,ObjectAssociations它們之間的關(guān)系:

至此,通過上圖可以看出它們之間的關(guān)系:
關(guān)聯(lián)對象并沒有添加在原來的類或者分類上,而是放在了一張哈希表中,將傳入的object轉(zhuǎn)化為disguised_object作為key,ObjectAssociationMap作為value放在了AssociationsHashMap表中,而ObjectAssociationMap表中存放的是以傳入的key作為key,ObjcAssociation作為value的數(shù)據(jù)關(guān)系,ObjcAssociation中保存的是傳入的value和policy。
那我們接下來看看它是這么取值的吧
objc_getAssociatedObject
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;
}
取值看起來就比較簡單了,通過disguised_object和key找到對應(yīng)ObjcAssociation,返回里面保存的value。這也就是為什么在取值的時候只需要傳入object和key,就可以取到值了。
objc_removeAssociatedObjects
void _object_remove_assocations(id object) {
vector< ObjcAssociation,ObjcAllocator<ObjcAssociation> > elements;
{
AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());
if (associations.size() == 0) return;
disguised_ptr_t disguised_object = DISGUISE(object);
//
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
// copy all of the associations that need to be removed.
ObjectAssociationMap *refs = i->second;
for (ObjectAssociationMap::iterator j = refs->begin(), end = refs->end(); j != end; ++j) {
elements.push_back(j->second);
}
// remove the secondary table.
delete refs;
associations.erase(i);
}
}
// the calls to releaseValue() happen outside of the lock.
for_each(elements.begin(), elements.end(), ReleaseValue());
}
上述源碼可以看出_object_remove_assocations函數(shù)將object對象所有關(guān)聯(lián)對象全部刪除。