IOS Runtime Exploration (v): Starting from runtime deep weak implementation mechanism

Source: Internet
Author: User

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.
weakModifiers 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

weakWhether 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 weakobject, 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.