One of the runtime runtimes: Message Delivery

Source: Internet
Author: User

what is runtime?

runtime, as the name implies. is the system runtime mechanism, it provides some of the objects can be passed between the message of the important function, the main thing is the message mechanism. Compared to the C language, the C language uses "static binding", the function of the call at compile time to know the runtime needs to call the function, after the completion of the compilation in order to execute (the process is so wayward). And OC is using the "dynamic binding" feature, that is, the compiler can not determine the compiler at the time of the runtime call which function, that is, in the compilation phase, OC can call any function, even if the function is not implemented, but as long as the declaration will not error, and C will be error (how can tolerate it indulgence, must error), After the object receives the message, exactly which method to call is determined by the runtime, and can even be changed when the program is run (this involves the "message forwarding" mechanism, which is described in the next article).

Principle of message passing mechanism of runtime correlation principle

Runtime involves a wide range of areas, this article only for the message delivery mechanism to expand this (finally began to focus ... )。 Okay, let's get to the point.

In OC, when a method of an object is called, it essentially sends a message to the object, such as:

ID returnvalue = [someobject messagename:paramater];

The above example calls the MessageName: method of the Someobject object, passes in a parameter paramater, and assigns the return value to the returnvalue of the ID type. Before introducing the messaging mechanism, let's first describe the core functions that are called in the message-passing mechanism:

void Objc_msgsend (id self, SEL cmd, ...)

The function is a variable function, can receive two or more parameters, the first parameter represents the method receiver, the second parameter with the choice of sub-selection (SEL is the type of the selector), the subsequent parameters are the corresponding parameters required by the method call.

The compiler will turn to the following C-language function after seeing the message from the previous example:

ID returnvalue = Objc_msgsend (Someobject, @selector (messagename:), paramater);

The Objc_msgsend function invokes the appropriate method based on the recipient and the selector . First in the cache of their own class to find the appropriate method, if found on the jump, if the cache does not find, then from the list of methods of the class to find, if found in the method list, then jump directly and add the method to the cache (the OC method is added to the cache after the first call, So that subsequent calls to the method can quickly find, improve the search speed); If the method list is not queried, then the inheritance system of the class that belongs to the object continues to look up , and then find the appropriate method to jump and add to the cache If the matching method is not found eventually, the message forwarding action is initiated (this article does not expand).

In each class there will be a table with the name of the selected child as the "key" for the lookup, each "key" corresponding to a unique "value", that is, the function implementation (IMP), each time through the corresponding "key" to find the corresponding value from the mapping table to return, This implementation can help us quickly navigate to the required functions.

One of the runtime runtimes: Message Delivery

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.