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
Find the implementation found by calling the specific implementation location of the selector Method Based on the Explorer Object, pass the parameter to regard the return value of the method implementation as its own return value, and return
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:
The pointer to the parent class is a scheduling table. The 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::
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.