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 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.

时间: 2024-10-13 09:46:20

isa objc_msgSend的相关文章

objc_msgSend消息传递学习笔记 – 消息转发

该文是 objc_msgSend消息传递学习笔记 – 对象方法消息传递流程 的基础上继续探究源码,请先阅读上文. 消息转发机制(message forwarding) Objective-C 在调用对象方法的时候,是通过消息传递机制来查询且执行方法.如果想令该类能够理解并执行方法,必须以程序代码实现出对应方法.但是,在编译期间向类发送了无法解读的消息并不会报错,因为在 runtime 时期可以继续向类添加方法,所以编译器在编译时还无法确认类中是否已经实现了消息方法. 当对象接受到无法解读的消息后

objc_msgSend消息传递学习笔记 – 对象方法消息传递流程

在Effective Objective-C 2.0 – 52 Specific Ways to Improve Your iOS and OS X Programs一书中,tip 11主要讲述了Objective-C中的消息传递机制.这也是Objective-C在C的基础上,做的最基础也是最重要的封装. Static Binding And Dynamic Binding C中的函数调用方式,是使用的静态绑定(static binding),即在编译期就能决定运行时所应调用的函数.而在Obje

《Effective Objective-C 2.0》—(第11-14条)—运行时动态绑定、objc_msgSend、消息转发机制

第11条:理解objc_msgSend的作用 在对象上调用方法是OC中经常使用的功能.用OC术语来说这叫做:"传递消息"(pass a message).消息有"名称"(name)或者"选择子"(selector),可以接收参数,而且可能还有返回值. 由于OC是C的超集,所以最好理解C语言的函数调用方式.C语言使用"静态绑定",就是说在编译期就能决定运行时所应调用的函数.以下列代码为例: #import <stdio.h

ios objc_msgSend函数(Objective-C消息机制的原理)

在Objective-C中,message与方法的真正实现是在执行阶段绑定的,而非编译阶段.编译器会将消息发送转换成对objc_msgSend方法的调用. objc_msgSend方法含两个必要参数:receiver.方法名(即:selector),如: [receiver message]; 将被转换为:objc_msgSend(receiver, selector); objc_msgSend方法也能hold住message的参数,如: objc_msgSend(receiver, sele

nil / Nil / NULL / NSNull VS objc_msgSend

[NSNull null]是一个对象,其类为NSNULL(isa):里面没有任何变量.函数.和实现. nil的处理展示出消息机制的优越性,相对于函数调用的空指针处理. ENTRY objc_msgSend # check whether receiver is nil teq     a1, #0 itt eq moveq   a2, #0 bxeq    lr # save registers and load receiver's class for CacheLookup stmfd  

不同ISA的一些特点

通常x86架构的字节码长度是不固定的,x64也也沿袭了这种做法. ARM架构则是具有固定长度的字节码. 在最开始的时候,所有的ARM指令都被编码为4字节.这被称为ARM mode. 后来发现可以压缩到两个字节.这被称为Thumb mode * Thumb mode和ARM mode可能会同时存在于一个程序中. 在ARMv7中,又出现了Thumb-2.Thumb-2则在Thumb之外又加入了一些长度为4字节的指令. ARM64的机器码则都是4字节长度的 * many other RISC ISAs

读书笔记 effective c++ Item 32 确保public继承建立“is-a”模型

1. 何为public继承的”is-a”关系 在C++面向对象准则中最重要的准则是:public继承意味着“is-a”.记住这个准则. 如果你实现一个类D(derived)public继承自类B(base),你在告诉c++编译器(也在告诉代码阅读者),每个类型D的对象也是一个类型B的对象,反过来说是不对的.你正在诉说B比D表示了一个更为一般的概念,而D比B表现了一个更为特殊的概念.你在主张:任何可以使用类型B的地方,也能使用类型D,因为每个类型D的对象都是类型B的对象:反过来却不对,也就是可以使

ISA Server 2006 软件防火墙管理

项目背景 公司是以生产制造企业,分有综合管理部.市场部.业务部.研发部门及制造部.为了适应市场的拓展,扩大公司网络规模,并希望拥有一套安全.高效.畅通的网络设施.安全.高效的网络,可以极大提高公司的办公效率.使公司安全高效的发展. ISA Server 2006 软件防火墙管理下载链接: http://down.51cto.com/data/2268161

IS-A 和 HAS-A

IS-A关系 IS-A就是说:一个对象是另一个对象的一个分类. 下面是使用关键字extends实现继承. public class Animal{ } public class Mammal extends Animal{ } public class Reptile extends Animal{ } public class Dog extends Mammal{ } 分析以上示例中的IS-A关系,如下: Mammal IS-A Animal Reptile IS-A Animal Dog