亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS中怎么關聯對象

發布時間:2021-07-26 14:26:15 來源:億速云 閱讀:172 作者:Leah 欄目:編程語言

這篇文章給大家介紹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中怎么關聯對象就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

ios
AI

白沙| 无棣县| 株洲县| 加查县| 伊宁县| 巧家县| 巴塘县| 黄山市| 连南| 项城市| 曲水县| 万年县| 新竹县| 高州市| 循化| 黎川县| 彭水| 陈巴尔虎旗| 乌什县| 逊克县| 梓潼县| 甘德县| 贺兰县| 新晃| 昂仁县| 安平县| 乐清市| 白城市| 河曲县| 仪征市| 黄浦区| 伊金霍洛旗| 黄梅县| 凌海市| 南江县| 云浮市| 滕州市| 镶黄旗| 万源市| 永吉县| 济阳县|