当前位置:网站首页>To get to the bottom: Principle Analysis of Objective-C correlation attribute
To get to the bottom: Principle Analysis of Objective-C correlation attribute
2022-07-19 10:59:00 【51CTO】
get to the bottom of the matter :Objective-C Principle analysis of correlation attribute
One . Introduction
Objective-C It is a dynamic language , So called dynamic capability , It can also be understood as runtime capability . about Objective-C For developers , The programming convenience brought by dynamics is everywhere . For example, through Category Class to extend the functions of existing classes . You can make existing classes have new methods and properties . however , If you use Category To extend the properties of the class , You must know that it is not easy to use @property Just make a statement . For example, the following code :
The code will not have any problems at compile time , But if you run , There will be undefined method exceptions . So if you want to extend the properties of a class , We usually do this :
Run again , Can be normal to addProperty Property to access the value . In fact, it is used here Objective-C Runtime features , stay Objective-C in , The memory space occupied by class objects has been determined when they are created , So have you ever thought about , adopt objc_setAssociatedObject How the attribute values stored by this runtime method are associated with the current object , Where do these data exist ? When lucky , from objc The source code can clearly understand the implementation logic of association attributes , This is also the focus of our article , Understanding the principles here may not help you much in using association properties , But this design idea will definitely benefit you a lot in daily development .
Two . objc_setAssociatedObject The core principle of the method
adopt objc Of runtime Source code , We can see objc_setAssociatedObject The method is as follows :
There is no need to explain this step too much , Just called an internal function ,_object_set_associative_reference Internal functions are the core of the implementation of correlation attributes , This function resolves as follows :
You can see , The whole process of associating attributes is very clear , Whether the new value needs retain And whether the old value needs release, It is determined by the correlation strategy :
acquireValue The method is as follows , It just judges whether it is necessary retain and copy, Then call the corresponding function :
On top of it 8 In step , Have call try_emplace Method to insert data into the table structure , When inserting this function, it will determine whether the data to be inserted exists , Its return value will inform the caller whether the insert operation has occurred , If it already exists , Then this function will do nothing .
3、 ... and . How to get and remove associated attributes
Now? , We have basically understood how Association attributes are set and stored , Then understand that it is very easy to get and remove .
Get the value of the associated attribute by using objc_getAssociatedObject The runtime method implements , This method is implemented as follows :
We will mainly analyze its call _object_get_associative_reference Internal methods :
For objects that have been associated with attributes , We can also call objc_removeAssociatedObjects Method to remove all associated attributes , This method is implemented as follows :
_object_remove_assocation The implementation of internal functions is not complicated , The analysis is as follows :
Four . How to manage memory with associated attributes ?
Through the previous introduction , We know that when associating attributes , You can set some options related to memory management through association policies , When setting association properties , If necessary , The old value will be adjusted internally according to the memory management policy release operation , But have you ever thought about , When the normal life cycle of an object ends , How is the memory occupied by these associated attributes recycled ? This requires us to start from the system dealloc Find the answer in the method .
System objects are destroyed ,dealloc The method will eventually execute to a named objc_destructInstance Inner function of , This function is implemented as follows :
It will determine whether the object to be destroyed has associated attributes , If there is , It will call again to _object_remove_assocation Function to remove the associated attributes , This function was introduced earlier , Internal processing of memory management issues .
5、 ... and . The creation time of association manager and table
In the whole Association attribute implementation scheme , There is another point that we have no closed-loop introduction , That is, the global association manager and Hash How tables are created , When was it created . We only see , When you want to set or get the associated property , Directly get the manager and Hash Table for use , No initialization . Actually , These global data structures are created in runtime Initialization is complete , The process path is as follows :
1. call runtime Entry function _objc_init
2. Notification call map_images function
3. call map_images_nolock function
4. map_images_nolock It will call arr_init function , This function is implemented as follows :
You can see , This function will automatically release the pool , Initialization of logic such as association attributes .
Focus on Technology , love your life , Communication technology , Also be friends .
meanwhile , If you find this article useful , Welcome to share with more friends , Please indicate the source .
边栏推荐
- How can enterprise telecommuting be more efficient?
- 2022/7/16
- Google Earth engine app (GEE) - set up a nighttime lighting timing analysis app in China
- Establishment of redis cluster, one master, two slave and three Sentinels
- High number__ Relationship between equation and function
- Redis集群、一主二从三哨兵的搭建
- mysql 查询报错
- How much money can you make by inventing flash memory? This is a Japanese dog blood story
- 基于网络编码的卫星网络容量提升方法
- LeetCode 2331. Calculate the value of Boolean binary tree (tree traversal)
猜你喜欢

腾讯云服务器利用镜像部署WordPress个人网站!

Journal日志与oplog日志的区别

LeetCode 2331. 计算布尔二叉树的值(树的遍历)

发明闪存能赚多少钱?这是一个日本的狗血故事

使用tesseract.js-offline识别图片文字记录

How much money can you make by inventing flash memory? This is a Japanese dog blood story

(1) Learn about MySQL

antd 下拉多选传值到后台做查询操作

论文笔记:Mind the Gap An Experimental Evaluation of Imputation ofMissing Values Techniques in TimeSeries

LeetCode 2315. Statistical asterisk (string)
随机推荐
Goldfish rhca memoirs: cl210 describes the openstack control plane -- identify the overcloud control platform service + chapter experiment
Svn learning
6G全域融合网络展望
空天地海一体化网络体系架构与网络切片技术
Pytorch手动实现多层感知机
OpenCV编程:OpenCV3.X训练自己的分类器
树链剖分思想讲解 + AcWing 2568. 树链剖分(dfs序 + 爬山法 + 线段树)
Pytorch框架 学习记录1 CIFAR-10分类
LeetCode 2331. Calculate the value of Boolean binary tree (tree traversal)
【设计过程】.NET ORM FreeSql WhereDynamicFilter 动态表格查询功能
Maximal semi connected subgraph (tarjan contraction + topological ordering + DP longest chain)
Mobile keyboard (simulation question)
leetcode-08
[in vivado middle note ILA IP core]
Vérification logique complexe personnalisée lors de l'ajout et de la modification - 2022 nouvel élément
Antd drop-down multiple options to transfer values to the background for query operations
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (2)
Avi Deployment Guide (2): overview of AVI architecture
LeetCode 2325. Decrypt message (map)
Opencv programming: opencv3 X trains its own classifier