OBJECTIVE-C Message delivery mechanism _c language

Source: Internet
Author: User

There are two kinds of methods in Objective-c language: ①selector②blocks, this article mainly said Selector, about Blocks will be summed up in the follow-up.

The Messaging model (message passing) is the core mechanism of objective-c language. In Objective-c, there is no way to invoke this assertion, only message delivery.

A method of calling a class in C + + or Java, in which a message is sent to the class in Objective-c. In C + + or Java, the relationship between a class and the behavior of a class is very close, a method must belong to a class, and is bound together at compile time, so you cannot invoke a method that is not in a class. In the case of Objective-c, the class and message are loosely coupled, and the method invocation simply sends a message to a class that can then determine how to handle the received message at run time. That is, a class is not guaranteed to respond to incoming messages. If you receive a message that cannot be processed, then the program is not going to go wrong or down, it is just doing nothing and returning a nil "the author added:" In the compilation period is not wrong, in line with the semantic understanding, but runtime runtime words , would crash. " This design itself is also more in line with the software metaphor. (very nice, seen from the Internet, copy came over)

Obviously, since the compiler does not locate the method, then only the Run-time positioning method, Objective-c is how to run the location of the position?

ID objc_msgsend (ID receiver, SEL selector, ...) "contains two necessary parameters: Receiver (Recipient object), selector (method selector), and an unknown parameter (selector parameter list)"

Objective-c is through the above method to find the Call method ~ such as [Itnoob cry] is converted to Objc_msgsend (itnoob,cry), where receiver is Itnoob object, selector is the cry selector, Of course, if the cry has parameters, it will also be converted, such as [Itnoob cry:@ "Vuvuzela" andsmile:@ "Xi Hee"] will be converted into objc_msgsend (itnoob,cry:andsmile:,@ "Hum", @ "Hee"), Similar to Objc_msgsend (ID receiver, SEL selector, Parm1,parm2,...).

Dynamic binding process of objc_msgsend

      according to the receiver object to find the implementation of the Selector method to find the implementation location call, pass the parameter to the method implementation of the return value as its own return value, return

That Objc_msgsend is how to find the specific implementation of the location, from the Internet to find a moment, as follows:

When the compiler constructs each class, each class must contain two necessary elements:

Pointer to the parent Class A dispatch table (dispatch table) that associates the selector of the class with the actual memory address of the method.

We know that each object has an Isa pointer to the owning class, and through this ISA pointer you can find the object's owning class and the parent class to which it belongs ...

The lookup process is as follows :

When you want to send a message to an object, first find the class that belongs to the ISA, and then look for the class's dispatch table, and if not, go to its parent class ... If found, the implementation is invoked based on the memory address in the dispatch table, if the return nil is not found at all.

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.