https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtHowMessagingWorks.html#//apple_ref/doc/uid/TP40008048-CH104-SW2

he objc_msgSend Function

In Objective-C, messages aren’t bound to method implementations until runtime. The compiler converts a message expression,

[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(receiver, selector)

Any arguments passed in the message are also handed to 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.

Note: The compiler generates calls to the messaging function. You should never call it directly in the code you write.

The key to messaging lies in the structures that the compiler builds for each class and object. Every class structure includes these two essential elements:

  • A pointer to the superclass.

  • A class dispatch table. This table has entries that associate method selectors with the class-specific addresses of the methods they identify. The selector for the setOrigin:: method is associated with the address of (the procedure that implements) setOrigin::, the selector for the display method is associated with display’s address, and so on.

When a new object is created, memory for it is allocated, and its instance variables are initialized. First among the object’s variables is a pointer to its class structure. This pointer, called isa, gives the object access to its class and, through the class, to all the classes it inherits from.

Note: While not strictly a part of the language, the isa pointer is required for an object to work with the Objective-C runtime system. An object needs to be “equivalent” to a struct objc_object (defined in objc/objc.h) in whatever fields the structure defines. However, you rarely, if ever, need to create your own root object, and objects that inherit from NSObject or NSProxy automatically have the isa variable.

These elements of class and object structure are illustrated in Figure 3-1.

Figure 3-1  Messaging Framework

When a message is sent to an object, the messaging function follows the object’s isa pointer to the class structure where it looks up the method selector in the dispatch table. If it can’t find the selector there, objc_msgSend follows the pointer to the superclass and tries to find the selector in its dispatch table. Successive failures cause objc_msgSend to climb the class hierarchy until it reaches the NSObject class. Once it locates the selector, the function calls the method entered in the table and passes it the receiving object’s data structure.

最新文章

  1. 解决VMware虚拟机宿主机与虚拟机通讯慢
  2. E 最熟悉的陌生人 (纪念当年就读的梅州市江南高级中学)
  3. 移动H5前端性能优化指南(转载)
  4. 你应该知道的2016年有关App开发的技术创新
  5. 解析 this.initialize.apply(this, arguments)
  6. Unity3D–Texture图片空间和内存占用分析(转载)
  7. [jquery]高级篇--js绑定事件
  8. 数据结构---顺序表(C++)
  9. javascript 函数声明问题
  10. 依赖注入及AOP简述(七)——FQCN请求模式
  11. 子请求执行失败。有关更多信息,请检查 InnerException。
  12. C# Linq to Entity 多条件 OR查询
  13. Node.js之mysql增删改查
  14. matlab : Nelder mead simplex 单纯形直接搜索算法;
  15. pthread_cond_wait() 函数的使用
  16. Git Note
  17. iOS 图文混排 (Swift版)
  18. poj3349 Snowflake Snow Snowflakes【HASH】
  19. 理解HTTP幂等性(转)
  20. Java IO------------------BIO(同步阻塞)、NIO1.0(多路复用)、NIO2.0(AIO,非阻塞)

热门文章

  1. Flume的Source、Sink总结,及常用使用场景
  2. FusionInsight大数据开发---Kafka应用开发
  3. Kafka MirrorMaker 跨集群同步工具
  4. 我的第一个netcore2.2 api项目搭建(一)
  5. delegate、Action、Func的用法
  6. @Valid注解的使用springmvc pojo校验
  7. 异常详细信息: System.Security.SecurityException: 未找到源,不过,未能搜索部分或所有事件日志。 若要创建源,您需要用于读取所有事件日志的权限以确保新的源名称是唯一的。 不可访问的日志: Security。
  8. 原生JS实现前端动画框架
  9. element-ui 上传图片或视频时,先回显在上传
  10. 49.react中使用less