Objective-C message transmission mechanism

Source: Internet
Author: User

In objective-C language, there are two ways to pass the method: ① selector ② blocks. This article mainly describes selector. blocks will be summarized later.

Message Passing is the core mechanism of objective-C language. In objective-C, there is no way to call this statement, but only message transmission.

Call methods of a class in C ++ or Java. In objective-C, a message is sent to the class. In C ++ or Java, the relationship between classes and behavior methods of classes is very close. A method must belong to a class and has been bound together at compilation, therefore, you cannot call methods that are not in a class. In objective-C, it is relatively simple. Classes and messages are loosely coupled. method calls only send a message to a class, this class can determine how to process received messages at runtime. That is to say, a class does not guarantee that it will respond to the received message. If it receives a message that cannot be processed, the program will neither make an error nor go down. It just does nothing, and return an Nil [I add: errors are not made during the compilation period, which is in line with the semantic understanding, but will crash when running runtime ]. This design is also in line with the software metaphor. (It's very nice. Copy it from the Internet)

Obviously, since the compiler does not locate the method, it only has the runtime locating method. How does objective-C locate the orientation during runtime?

Id objc_msgsend (ID receiver, Sel selector ,...) it includes two necessary parameters: the receiver object, the selector (method selector), and an unknown parameter (the selector parameter list )]

Objective-C is used to find the call method through the above method ~ For example, [itnoob cry] is converted to objc_msgsend (itnoob, CRY). Here, the handler is the itnoob object, and the selector is the cry selector. Of course, if the cry has parameters, it will be converted, for example, [itnoob cry: @ "" andsmile: @ ""] will be converted to objc_msgsend (itnoob, cry: andsmile:, @ "", @ ""), for example, objc_msgsend (ID generator er, Sel selector, parm1, parm2 ,...).

Dynamic binding process of objc_msgsend

  1. Find the specific implementation location of the selector Method Based on the Explorer Object
  2. Call the found implementation and PASS Parameters
  3. Use the return value of the method implementation as its own return value.

Then how does objc_msgsend find the specific implementation location of the method? I found it from the Internet, as shown below:

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

  • Pointer to the parent class
  • A scheduling table associates the selector of the class with the actual memory address of the method.

We know that each object has an ISA pointer pointing to its class. Through this isa pointer, we can find the class to which the object belongs and its parent class...

The search process is as follows:

(Image Source Network)

When you want to send a message to an object, first locate the class based on ISA, and then find the dispatch table of the class. If not found, search for it in its parent class... if yes, call the implementation based on the memory address in the scheduling table. If no NIL is found at the end, the NIL is returned.

Reprint Please note: it relocation brick juvenile center http://cnblogs.com/ItNoob

 

By zxzhao

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.