消息发送
This chapter describes how the message expressions are converted into objc_msgSend function calls, and how you can refer to methods by name. It then explains how you can take advantage of objc_msgSend, and how—if you need to—you can circumvent dynamic binding.
这一章描述了消息发送在编译时如何转换成objc_msgSend函数调用,以及你如何通过方法名来找到对应的方法。然后解释如何利用objc_msgSend,,如果你需要你可以绕过动态绑定。
objc_msgSend函数
In Objective-C, messages aren’t bound to method implementations until runtime. The compiler converts a message expression,
在objective - c中,直到运行时消息才会和对应实现的方法绑定。消息调用时的转换是在编译期间进行的.
[receiver message]
into a call on a messaging function, objc_msgSend. This function takes the receiver and the name of the method mentioned in the message—that is, the method selector—as its two principal parameters:
objc_msgSend是一个消息发送传递的函数,。这个函数需要消息接收者和消息方法名(这个方法名选择器) 作为它的两个主要参数:
objc_msgSend(receiver, selector)
Any arguments passed in the message are also handed to objc_msgSend:
消息传入的任何参数也交给objc_msgSend:
objc_msgSend(receiver, selector, arg1, arg2, ...)
The messaging function does everything necessary for dynamic binding:
消息传递函数是动态绑定所需的一切:
It first finds the procedure (method implementation) that the selector refers to. Since the same method can be implemented differently by separate classes, the precise procedure that it finds depends on the class of the receiver.
It then calls the procedure, passing it the receiving object (a pointer to its data), along with any arguments that were specified for the method.
Finally, it passes on the return value of the procedure as its own return value.
1,它首先找到 SEL 对应的方法实现 IMP。因为不同的类对同一方法可能会有不同的实现,所以找到的 方法实现依赖于消息接收者的类型。
2, 然后将消息接收者对象(指向消息接收者对象的指针)以及方法中指定的参数传递给方法实现 IMP。
3, 最后,将方法实现的返回值作为该函数的返回值返回。
Note: The compiler generates calls to the messaging function. You should never call it directly in the code you write.
注:编译器自动生成调用消息传递功能。你不应该直接在您编写的代码去实现。
未完待续………..