In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the example analysis of related objects in OC runtime. I hope you will get something after reading this article. Let's discuss it together.
Principle of runtime Associated objects
The API provided in runtime is as follows:
/ / object association policy typedef OBJC_ENUM (uintptr_t, objc_AssociationPolicy) {OBJC_ASSOCIATION_ASSIGN = 0, / * *
< 关联一个弱引用 */ OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< 指定关联一个强引用. 非原子性. */ OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< 指定关联一个拷贝引用. 非原子性. */ OBJC_ASSOCIATION_RETAIN = 01401, /**< 指定关联一个强引用.原子性. */ OBJC_ASSOCIATION_COPY = 01403 /**< 指定关联一个拷贝引用.原子性. */};/** * 用给定的 key 和关联策略 policy 为指定的 object 设置关联关系 */void objc_setAssociatedObject(id object, const void * key, id value, objc_AssociationPolicy policy);/** * 通过给定的 object 和 key 拿到关联的对象 */id objc_getAssociatedObject(id object, const void * key);/** * 移除给定 object 所有关联的关系 */void objc_removeAssociatedObjects(id object); runtime 的 API 很简单,实际上工作中使用 runtime 的关联对象也很方便,如下: static const void * _Nonnull titleKey = @"titleKey";- (void)setTitle:(NSString *)title{ objc_setAssociatedObject(self, titleKey, key, OBJC_ASSOCIATION_RETAIN);}- (NSString *)title{ return objc_getAssociatedObject(self, titleKey);} 如此简单强大的功能,如果想要观察系统的实现,可以找关联对象的源码,我们可以从objc 源码中拿到, 从源码中,可以找到以上三个 API 的实现如下: 以 objc_setAssociatedObject 方法为例,方法内部调用的一个内部方法,参数同上,进入内部方法 /// 方法为我加过注释的代码。void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy) { // 取原来的绑定关系,新绑定关系原来关系为空 ObjcAssociation old_association(0, nil); // 验证参数 id new_value = value ? acquireValue(value, policy) : nil; { // 内部绑定关系管理器.内部管理一个全局 AssociationsHashMap AssociationsManager manager; // 管理器中的绑定关系Map AssociationsHashMap &associations(manager.associations()); // 生成伪装地址。处理参数 object 地址 disguised_ptr_t disguised_object = DISGUISE(object); if (new_value) { // 打破原来存在的绑定关系 AssociationsHashMap::iterator i = associations.find(disguised_object); // 以伪装地址为key 在AssociationsHashMap找objc 对应的二级 Map if (i != associations.end()) { // 二级Map存在。并取二级map: ObjectAssociationMap ObjectAssociationMap *refs = i->Second; / / find the binding relation in ObjectAssociationMap ObjectAssociationMap::iterator j = refs- > find (key); / / find ObjcAssociation if (j! = refs- > end ()) {/ / find the original ObjcAssociation relation in ObjectAssociationMap through key, and directly assign old_association old_association = j-> second; j-> second = ObjcAssociation (policy, new_value) } else {/ / does not exist-> re-create (* refs) [key] = ObjcAssociation (policy, new_value);}} else {/ / if there is no second-level Map, directly create ObjectAssociationMap * refs = new ObjectAssociationMap; associations [discharges _ object] = refs; (* refs) [key] = ObjcAssociation (policy, new_value); object- > setHasAssociatedObjects ();} else {/ / No value, set the association to nil directly. Remove the original binding relationship 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 old value. If (old_association.hasValue ()) ReleaseValue () (old_association);}
From the source code, you can see that there are four main objects associated with maintenance objects.
AssociationsManager / / global manager who maintains the relationship-> Save AssociationsHashMap
AssociationsHashMap / / the HashMap that saves the association relationship-- > the Map that saves the association relationship corresponding to the object
Map of ObjectAssociationMap / / object association relationship, second-level Map
ObjcAssociation / / association relationship
Their relationship is graphically shown as follows:
Runtime
It is equivalent to recreating and managing a set of object association mechanism from outside our objects, and maintaining the relationship between objects through two-layer hashMap. Where AssociationsHashMap (outer Map) is a global static map stored in AssociationsManager. By processing the memory address of the associated object, the key that is uniquely bound to the object stores the ObjectAssociationMap associated with the object in map, which in turn maintains an ObjcAssociation (association policy and value).
The above takes objc_setAssociatedObject as an example to explain the principle of associated objects, and the principles of objc_getAssociatedObject and objc_removeAssociatedObjects are the same.
Thinking expansion
The principle of object association in runtime, as above, learned the idea of Apple source code. You can find that many similar data stores in your work can be used for reference.
Just in the afternoon when I was studying the principle of runtime object association, my friend asked me about the implementation logic of a multi-user system.
The main idea is:
After logging in through the mobile phone number, the user will generate a user information table in memory, which can store the user's basic information, contact information, wallet information.
After the user logs out, the information will not be lost.
After other users log in, the corresponding user information table will also be created and saved.
After logging in before, users can still view their previous user information.
Seeing this problem, I directly feel that the implementation of object association in runtime happens to be an excellent implementation of this problem.
Create a user information Map for key by logging in to id. Make sure that each user id corresponds to a unique user information table, and customize what information you want to save in the user information table.
After reading this article, I believe you have some understanding of "sample Analysis of related objects in OC runtime". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.