iOS Development note--runtime that little thing.

Source: Internet
Author: User

Objective-c the whole runtime thing. (a) message mechanism

Recently looking for a job, objective-c in the runtime is often asked a question, almost is the interview big company must ask a question. There are, of course, some other questions that are almost certainly asked, such as Runloop,block, memory management, and so on. Other questions I will introduce in other articles if I have the opportunity. This article mainly introduces the runtime.

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 {Class isa objc_isa_availability;}

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

typedef struct objc_class *class;struct objc_class {  class isa;    Point metaclass    class super_class ; //  to its parent class   const  char *name ; //  class name   long version ; //  class version information, initialization defaults to 0, Can be modified, read   long info; //  some identifying information through the runtime functions class_setversion and class_getversion, such as Cls_ class  (0x1l)   indicates that the class is a normal  class&nbsp, which contains object methods and member variables; cls_meta  (0x2l)   indicates that the class is  metaclass, which contains class methods;   long instance_size ; //   instance variable size of the class (including instance variables inherited from the parent class);  struct objc_ivar_list *ivars; //  is used to store the address of each member variable   struct objc_method_list **methodLists ; //  related to some of the  info  's flag bits, such as the CLS _class  (0x1l), the storage-class method is stored as an object method, such as cls_meta  (0X2L);   struct objc_cache *cache; / /  pointer to the nearest method used to increase efficiency;   struct objc_protocol_list *protocols; //  stores the protocol    &nbsp the class adheres to;} 

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.

Original address: http://www.cocoachina.com/ios/20141018/9960.html

iOS Development note--runtime that little thing.

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.