iOS 开发 深入浅出Runtime运行时之官方指南翻译--消息发送

本文详细介绍了Objective-C中的消息发送机制,重点解析objc_msgSend函数的工作原理,包括如何通过方法名查找方法实现,以及消息传递的三个关键步骤。文章适合iOS开发者深入理解Runtime和动态绑定。
摘要由CSDN通过智能技术生成
  1. 苹果官方消息发送链接地址

消息发送

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.

注:编译器自动生成调用消息传递功能。你不应该直接在您编写的代码去实现。

未完待续………..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值