Objective-c source Code (iii) The realization principle of category

Source: Internet
Author: User

There are 3 main usage scenarios for Category:

Add a method to an existing class;

Splitting the implementation of a class into multiple independent source files;

Declares a private method.

Implementation Principle :

We know that regardless of whether we have actively introduced category header files, the methods in the category will be added to the main class. We can use the-performselector: The corresponding method in the category to call, the reason for the need to introduce the category header file in the call, just to "look after" the feelings of the compiler classmate.

Opens the objc-runtime-new.mm file to find the _read_images () method

if  (cat->instancemethods | |   cat->protocols                   | |   cat->instanceproperties)               {                 Addunattachedcategoryforclass (Cat, cls, hi);                 if  (cls->isrealized ())  {                     remethodizeclass (CLS );                     classExists = YES;                 }                if  (printconnecting)  {                     _ Objc_inform ("class: found category -%s (%s)  %s",                                    cls->nameforlogging (), cat->name,                                    classExists ?  " On existing class " : ");                 }            }             if  (cat->classmethods  | |   cat->protocols                   /* | |   cat->classproperties */)               {                 Addunattachedcategoryforclass (Cat, cls->isa (),  hi);                 if  (Cls->isa ()->isrealized ())  {                      remethodizeclass (Cls->isa ());                 }              & NBsp; if  (printconnecting)  {                     _objc_inform ("class: found category +%s (% s) ",                                   cls-> Nameforlogging (),  cat->name);                 }            }

You can see that this function does the following with the category:

Register the category and its main class (or meta-class) in a hash table;

If the main class (or meta Class) has been implemented, then rebuild its list of methods.

This is done in two cases: the instance methods and attributes in the category are integrated into the main class, and the class methods are integrated into the Meta class. In addition, the processing of the protocol is very special, the protocol in the category is integrated into the main class and the Meta class.

The end result is by calling

Remethodizeclass (Class CLS)

The main function of this function is to integrate the methods, properties, and protocols in the category into the class (main class or Meta Class), updating the values of method_lists (or Method_list), properties, and protocols in the data field of the class. Further, we can find the attachmethodlists function that really deals with the Category method through the source code of the Attachcategorymethods function.

Its main function is to combine the concentrated long-class method and the newly added method in the category into a list of methods of the heart, and assign the value to Method_lists or method_list. Description: The methods in the main class and the methods in the category do not seem to differ from runtime, they are treated equally, and are stored in the method list of the main class.

Runtime does not specifically deal with the +load method in the process of the method in Category. Therefore, the +load method in the category strictly means that the +load method in the main class will be overwritten as well as the normal method, except that runtime is called directly using the pointer of the respective method when the +load method in the main class and category is automatically invoked. So it makes us think that the +load methods in the main class and category do not seem to affect each other. Therefore, when we manually send a +LOAD message to the main class, the call will always be the +load method in the taxonomy.


Objective-c source Code (iii) The realization principle of category

Related Article

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.