iOS關(guān)聯(lián)對(duì)象示例詳解
背景
在iOS開發(fā)中如果我們想給一個(gè)對(duì)象動(dòng)態(tài)添加屬性或者給category添加屬性的時(shí)候,都是通過runtime的關(guān)聯(lián)對(duì)象去實(shí)現(xiàn),那我們添加的屬性到底是如何存取的呢?是直接添加到了對(duì)象自身的內(nèi)存中了去嗎?帶著這些疑問讓我們看一runtime的源碼,解開關(guān)聯(lián)對(duì)象的神秘面紗。
關(guān)聯(lián)對(duì)象源碼
存值
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) { _object_set_associative_reference(object, (void *)key, value, policy); }
我們調(diào)用此方法的時(shí)候,一共傳遞了四個(gè)參數(shù):
參數(shù)名稱 | 解釋 |
---|---|
id object | 需要關(guān)聯(lián)的對(duì)象 |
void *key | 對(duì)應(yīng)的key |
id value | 對(duì)應(yīng)的值 |
objc_AssociationPolicy policy | 內(nèi)存管理策略 |
內(nèi)存管理策略:
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. */ };
對(duì)于四個(gè)參數(shù)理解完了之后讓我們看看它真正的實(shí)現(xiàn)函數(shù)_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);//得到對(duì)象地址 if (new_value) { // break any existing association. AssociationsHashMap::iterator i = associations.find(disguised_object);//首先通過對(duì)象的地址獲取對(duì)象的hashmap if (i != associations.end()) {//判斷是否已經(jīng)存在,已經(jīng)存在 // secondary table exists ObjectAssociationMap *refs = i->second;//取值,對(duì)應(yīng)的map ObjectAssociationMap::iterator j = refs->find(key);//通過key查找 if (j != refs->end()) {//如果已經(jīng)存在 old_association = j->second;//取到原來老的值,以便后邊對(duì)其釋放 j->second = ObjcAssociation(policy, new_value);//存儲(chǔ)新的值 } else {//不存在 (*refs)[key] = ObjcAssociation(policy, new_value); } } else {//如果不存在,創(chuàng)建一個(gè) // create the new association (first time). ObjectAssociationMap *refs = new ObjectAssociationMap; associations[disguised_object] = refs; (*refs)[key] = ObjcAssociation(policy, new_value); object->setHasAssociatedObjects(); } } else {//不存在則創(chuàng)建一個(gè) // 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); }
通過以上代碼我們可以看出其實(shí)關(guān)聯(lián)對(duì)象在存儲(chǔ)的時(shí)候在,生成了一個(gè)AssociationsManager單例對(duì)象,所以應(yīng)用中所有的管理對(duì)象都存儲(chǔ)于此AssociationsManager中。
具體存儲(chǔ)的實(shí)現(xiàn)是借助了C++的關(guān)聯(lián)容器unordered_map實(shí)現(xiàn)的。具體可以參看代碼中我加的注釋。
整個(gè)過程就是通過object對(duì)象的地址存儲(chǔ)了一個(gè)類似hashmap的東西;取到此hashmap,然后通過鍵值對(duì)的方式將我們需要存儲(chǔ)的值存儲(chǔ)到此hashmap中,這個(gè)過程中如果有舊值,則最后會(huì)將舊值就行釋放
取值
取值的過程其實(shí)就比較簡單了,就相當(dāng)于從一個(gè)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; }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS實(shí)現(xiàn)翻頁效果動(dòng)畫實(shí)例代碼
本篇文章主要介紹了iOS實(shí)現(xiàn)翻頁效果動(dòng)畫實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05iOS 使用 socket 實(shí)現(xiàn)即時(shí)通信示例(非第三方庫)
這篇文章主要介紹了iOS 使用 socket 即時(shí)通信示例(非第三方庫)的資料,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-02-02iOS使用 CABasicAnimation 實(shí)現(xiàn)簡單的跑馬燈(無cpu暴漲)
本篇文章主要介紹了iOS使用 CABasicAnimation 實(shí)現(xiàn)簡單的跑馬燈(無cpu暴漲),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01iOS正則表達(dá)式驗(yàn)證手機(jī)號(hào)、郵箱、身份證號(hào)等
這篇文章主要介紹了iOS正則表達(dá)式驗(yàn)證手機(jī)號(hào)、郵箱、身份證號(hào)等信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Flutter之PageView頁面緩存與KeepAlive
這篇文章主要為大家介紹了Flutter之PageView頁面緩存與KeepAlive示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10iOS11 WKWebView 無法加載內(nèi)容的解決方法
這篇文章主要介紹了iOS11 WKWebView 無法加載內(nèi)容,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11