Runtime in the Objective-c

Source: Internet
Author: User



runtime abbreviation. is the system at the time of the operation of some mechanism, the most important is the message mechanism. For the C language, the function's call will decide which function to call at compile time (see here for the C-language function call). The sequence executes directly after compilation, without any ambiguity. The function call of OC becomes a message sent. belongs to the dynamic call procedure. It is not possible at compile time to decide which function to actually call (it turns out that OC can call any function during the compile phase, even if the function is not implemented, as long as it is declared without an error.) C language will be in the compilation phase error). The corresponding function will be called based on the name of the function only when it is actually running.



How does OC implement dynamic invocation? Let's take a look at how OC achieves the secret of dynamic calls by sending messages. If you write such a code in OC:


[obj makeText];


Where obj is an object and Maketext is a function name. For such a simple invocation. Runtime at compile time will convert the above code into



objc_msgSend(obj,@selector(makeText));


First, let's take a look at the Obj object, which is inherited from NSObject in iOS.


@interface NSObject <nsobject> {    Class isa  OBJC_ISA_AVAILABILITY;}</nsobject>


There is a class Isa pointer in the NSOBJCET. Then we look at class:


typedef struct objc_class * Class; struct objc_class {Class isa; // points to metaclass Class super_class; // points to its parent class const char * name; // class name long version; // class version information, initialization defaults to 0, can Modify and read long info through runtime functions class_setVersion and class_getVersion; // some identification information, such as CLS_CLASS (0x1L) indicates that the class is an ordinary class, which contains object methods and member variables; CLS_META (0x2L) indicates that the class is metaclass, It contains class methods; long instance_size; // the size of instance variables of this class (including the instance variables inherited from the parent class); struct objc_ivar_list * ivars; // struct objc_method_list ** methodLists for storing the address of each member variable; // Related to some flags of info, such as CLS_CLASS (0x1L), store object methods, such as CLS_META (0x2L), store class methods; struct objc_cache * cache; // Pointer to the most recently used method, used for promotion Efficiency; struct objc_protocol_list * protocols; // store the protocol that this class complies with}





As we can see, there are a lot of things in a class, and here's a one by one explanation:



Class Isa: Point to Metaclass, which is the static class. In general, ISA in an Obj object points to the normal class, which stores ordinary member variables and object methods (the "-" method at the beginning of the "-"), the ISA pointer in the normal class to the static class, and the static class that stores statically type member variables and class methods (" + "starts with the method).



Class Super_class: Points to the parent class, or null if the class is a root class.



The following picture is a good description of the inheritance relationship of classes and objects:






Note : All metaclass in the ISA pointer are pointed to the metaclass. And the metaclass point to itself. Root Metaclass is generated by inheriting the root class. is consistent with the root class struct member, which is the structure mentioned earlier. The difference is that the ISA pointer to root metaclass points to itself.



The rest of the class is not explained here, so let's take a look at the following:



@selector (Maketext): This is an Sel method selector. The main function of SEL is to quickly find the corresponding method's function pointer by means of the method name (Maketext), and then call its function. The SEL itself is an address of type int, with the name of the method stored in the address. For a class. Each method corresponds to an sel. So there cannot be 2 identical methods in the iOS class, even if the parameter types are different because the SEL is generated from the method name, the same method name can only correspond to one sel.



Let's take a look at how the specific message is sent after it's been dynamically searched for the corresponding method.



First, the compiler converts the code [obj Maketext] into objc_msgsend (obj, @selector (Maketext)), in the Objc_msgsend function. The class of obj is first found through the ISA pointer of obj. In class, first go to the cache through the SEL to find the corresponding function method (guess the Cache method list is the SEL key through the hash table to store, this can improve the function lookup speed), if not found in the cache. Again to find in the methodlist, if not found in MethodList, then take superclass to find. If found, the method is added to the cache to facilitate the next lookup, and the function pointer in method jumps to the corresponding function to execute.



Runtime in the Objective-c


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.