Messaging mechanisms and message forwarding for iOS

Source: Internet
Author: User

1. Messaging mechanisms

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.

[obj Maketext];

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.

2. Message forwarding (multiple inheritance can be implemented indirectly)

When a message is sent to Someobject, but the runtime system cannot find the implementation of the corresponding method in both the current class and the parent class, the runtime system does not immediately make an error to crash the program, but instead performs the following steps in turn:

1. Dynamic method parsing: Sends a resolveinstancemethod: signal to the current class to check whether a method has been added to the class dynamically. (Search for confusion: @dynamic)

2. Fast message forwarding: Check whether the class implements the Forwardingtargetforselector: method, or call this method if it is implemented. If the method returns a value object that is non-nil or non-self, resend the message to the returned object.

3. Standard message forwarding: Runtime sends Methodsignatureforselector: Message gets selector corresponding method signature. The return value is non-null by forwardinvocation: Forwards the message, and the return value is null to send doesnotrecognizeselector: Message to the current object, and the program crashes exiting. Here is a code demonstration of only 2: meaning that the method cannot be implemented in this class and can be forwarded to another class to implement
-(ID) Forwardingtargetforselector: (SEL) aselector{    Class *other = [Class new];    if ([other forwardingtargetforselector:aselector]) {        return to other;    } else{        return [Super Forwardingtargetforselector:aselector];}    }

This way, if the method is not in this class, it will go to the class to try to find, if there is, then return the class object, let it execute, so that the rapid message forwarding

Messaging mechanisms and message forwarding for iOS

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.