isa objc_msgSend
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 thedisplaymethod is associated withdisplay’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.
isa objc_msgSend的更多相关文章
- objc_msgSend消息传递学习笔记 – 消息转发
该文是 objc_msgSend消息传递学习笔记 – 对象方法消息传递流程 的基础上继续探究源码,请先阅读上文. 消息转发机制(message forwarding) Objective-C 在调用对 ...
- objc_msgSend消息传递学习笔记 – 对象方法消息传递流程
在Effective Objective-C 2.0 – 52 Specific Ways to Improve Your iOS and OS X Programs一书中,tip 11主要讲述了Ob ...
- 初探 objc_msgSend函数
1.0 执行某个对象的方法 [receiver message] 被编译为: id objc_msgSend(id self,SEL op,...): objc_msgSend 发送信息的过程 ...
- objc_msgSend函数的实现
毕竟汇编语言代码比较晦涩难懂,因此这里将函数的实现反汇编成C语言的伪代码: //下面的结构体中只列出objc_msgSend函数内部访问用到的那些数据结构和成员. /* 其实SEL类型就是一个字符串指 ...
- 神经病院Objective-C Runtime入院第一天——isa和Class
前言 我第一次开始重视Objective-C Runtime是从2014年11月1日,@唐巧老师在微博上发的一条微博开始. 这是sunnyxx在线下的一次分享会.会上还给了4道题目. 这4道题 ...
- Runtime之IMP指针,isa指针
要了解 isa 指针先了解下类的定义在xcode中用快捷键Shift+Cmd+O 搜索objc.h 能看到类的定义:了解 Paste_Image.png 可以看出:objc_object:Object ...
- nil / Nil / NULL / NSNull VS objc_msgSend
[NSNull null]是一个对象,其类为NSNULL(isa):里面没有任何变量.函数.和实现. nil的处理展示出消息机制的优越性,相对于函数调用的空指针处理. ENTRY objc_msgSe ...
- 反汇编分析objc函数枢纽objc_msgSend
在分析objc_msgSend之前,先来搞清楚另一个问题. 函数是什么?可能会答 void foo(void) {} 像这样就是一个函数.或者函数包括函数原型和函数定义,是一段执行某样功能的机器代码. ...
- 如何正确的hook方法objc_msgSend · jmpews
如何正确的hook方法objc_msgSend 前言 如果希望对 Objective-C 的方法调用进行 log, 一个很好的解决方法就是 hook 方法 objc_msgSend, 当然想到的就是利 ...
随机推荐
- 查看linux系统版本及内核
一.查看Linux系统版本的命令(3种方法) 1.适用于所有的Linux发行版 cat /etc/issue [root@S-CentOS home]# cat /etc/issue CentOS r ...
- java8新特性七-Date Time API
Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理. 在旧版的 Java 中,日期时间 API 存在诸多问题,其中有: 非线程安全 − java.ut ...
- 由一个问题引起的思考:WEB开发中,使用JSON-RPC好,还是RESTful API好?
起因: 研究zabbix的API设计风格.查看zabbix官网API文档,可以看到使用的是json-rpc:2.0 随后搜索到知乎上的一个问题讨论:https://www.zhihu.com/ques ...
- idea(2018.3.5)破解
第一步:下载idea,https://www.jetbrains.com/idea/download/#section=windows,双击进行安装 第二步:下载破解的jar包:链接:https:// ...
- springmvc流程图以及配置
springmvc:是完成数据的封装和跳转的功能 流程图如下: springmvc的配置流程 1.导入jar包 二.配置servlet文件 init-param的作用是在启动servlet启动时规定其 ...
- 复习:C语言基础知识1
占位符: %d, %i,代表整数,%f-浮点,%s,字符串,%c,char. %p 指针,%fL 长long,%e科学计数,%g 小数或科学计数. C语言中的格式占位符: %a,%A 读入一个浮点值( ...
- 整理:WPF用于绑定命令和触发路由事件的自定义控件写法
原文:整理:WPF用于绑定命令和触发路由事件的自定义控件写法 目的:自定义一个控件,当点击按钮是触发到ViewModel(业务逻辑部分)和Xaml路由事件(页面逻辑部分) 自定义控件增加IComman ...
- efCore+Mysql+Net Core
1.首先新建一个空的Asp.net core项目 2.新建一个类 gj.cs public class gj { // <summary> /// 主键 /// </summa ...
- 开源串口 Ymodem 上位机软件
概述 上位机使用Qt开发,计划整合多个工具为一体,用作以后的调试工具. 当前完成功能: 1.串口调试 支持hex和ascii 码发送,接受. 支持自动添加回车换行. 支持定时发送,最短间隔100ms, ...
- Netty高性能原理和框架架构解析
1.引言 Netty 是一个广受欢迎的异步事件驱动的Java开源网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端. 本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件 ...