You know, the runtime is here.
Reprint please specify the source http://blog.csdn.net/u014205968/article/details/67639341
This article mainly explains the runtime
related knowledge, from the principle to the practice, because contains the content to be divided into the following five articles to explain in detail, may choose the direction which needs to understand by oneself:
- Starting from Runtime: Understanding object-oriented classes to process-oriented structures
- Starting from Runtime: in-depth understanding of the OC message forwarding mechanism
- Starting from Runtime: Understanding the properties of OC
- Starting from Runtime: Practice category Add attribute and black Magic method swizzling
- Starting from Runtime: in-depth weak implementation mechanism
This article is the fifth article of a series of articles, but also the last of a series of articles starting from Runtime: in-depth weak implementation mechanism, this article mainly explains runtime
How to implement weak
modifiers.
weak
Modifiers We're not strangers at all. Often used in development, the most important role is to prevent 引用循环(retained cycle)
, often used block
and delegate
, in the previous several articles have been explained weak
the basic use and reference cycle, if interested can refer to the article iOS @property Explore (a) : basic details, iOS @property Exploration (ii): In-depth understanding, iOS block Inquiry (a): basic details, iOS block Inquiry (ii): In-depth understanding, related methods and features this article will not repeat.
Weak
weak
Whether it is used as property
a modifier or as a declaration that modifies a variable, its effect is the same, it does not increase the reference count of the new object, and is freed without reducing the reference count of the new object, and when the new object is destroyed, the weak
decorated property or variable is set to nil
, This can prevent the wild pointer error, this article is to explain this feature, runtime
How to weak
Modify the object of the variable is automatically set when the destruction nil
.
So runtime
how do you implement an object in a weak
modified variable that is automatically set when it is destroyed nil
? A common explanation is that the runtime
registered class will be laid out, and the weak
decorated object will be placed hash表
in one. With the object weak
memory address pointed to key
, when the reference count of this object is 0, dealloc
if the weak
point of the object memory address is a
, then will be the a
key in this weak表
search, to find all the keys for a
the weak
object, which is set to nil
.
With this knowledge, you can drill down into runtiem
the code to see the specifics of the implementation, and interested readers can continue to read.
Deep Runtime Understanding Weak notes
Due to the limited level of the author, there are inevitably flaws, if there are problems please enlighten.
IOS Runtime Exploration (v): Starting from runtime deep weak implementation mechanism