objc_msgSend()分配表 [英] objc_msgSend() dispatch table
问题描述
我需要清除一些混乱以了解objc_msgSend().请足够慷慨地提供详细答案.:)
I need to clear some clutter in my mind to understand objc_msgSend(). Kindly be generous enough to give details answers please. :)
这是我对Apple Doc的了解.
Here is my understand from Apple Doc.
将消息发送到对象时,消息传递功能如下对象的isa指针,指向要在其中查找的类结构调度表中的方法选择器.如果找不到选择器在那里,objc_msgSend()跟随指向超级类的指针(此处为混乱)并尝试爬上类层次结构.
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 cant find the selector there, objc_msgSend() follows the pointer to the supperclass (HERE is confusion) and tries to climb the class hierarchy.
在这里,超类是否获得分配的内存,并且该超类的isa指针可用?以上引用说明了这一点吗?
Here, does the superclass get memory allocated and isa pointer available for that superclass? Does the above quote says that?
推荐答案
如果查看NSObject的ObjC-2.0之前的声明,您会看到:
If you were look at the pre-ObjC-2.0 declaration of NSObject, you'd see:
@interface NSObject <NSObject> {
Class isa;
}
班级在哪里:
typedef struct objc_class *Class;
objc_class是:
And objc_class is:
struct objc_class {
struct objc_class *isa;
struct objc_class *super_class;
const char *name;
long version;
long info;
long instance_size;
struct objc_ivar_list *ivars;
struct objc_method_list **methodLists;
struct objc_cache *cache;
struct objc_protocol_list *protocols;
};
因此,该 isa
指针(即 NSObject
中的第一个字段,也是每个子类中的第一个字段)是指向该 objc_class
结构.结构的其余部分描述了该类的详细信息. super_class
字段指向描述超类的 objc_class
结构.
Thus, that isa
pointer -- the first field in NSObject
, but also the first field in every subclass -- is a pointer to that objc_class
structure. The rest of the structure describes the details of that class. That super_class
field points to the objc_class
structure that describes the superclass.
当 objc_msgSend()
尝试将方法分派给 NSMutableString
时,它首先浏览 objc_class
的方法列表描述 NSMutableString
实例.如果找不到该方法,它将查看 super_class
结构并在其中搜索.如果找不到,则在 super_class
的 super_class
中查找.
When objc_msgSend()
tries to dispatch a method to, say, an NSMutableString
, it first looks through the method list of the objc_class
describing an NSMutableString
instance. If it doesn't find the method, it looks at the super_class
structure and searches there. If it can't find it there it then looks in the super_class
's super_class
.
即任何给定类的 isa
所指向的结构中的 super_class
指针是整个类层次结构中直至 NSObject
的链接列表(通常).如果 super_class
字段为空,则将其作为根类.
I.e. the super_class
pointer in the structure pointed to by the isa
of any given class is a linked list up the class hierarchy all the way to NSObject
(usually). If the super_class
field is empty, that'd be a root class.
或多或少地以相似的方式完成了内存分配.实例的 instance_size
字段指定实例的大小,从而指定要分配的内存量.
Memory allocation is, more or less, done in a similar fashion. The instance_size
field of the instance specifies the size of the instance and, thus, the amount of memory to be allocated.
或多或少.所有这些结构现在都是不透明的.正如理查德·罗斯三世(Richard Ross III)在评论中所说,您必须使用自省功能来获取这些结构的内容.这样一来,Apple便可以更改实施细节,而无需重新编译所有内容.
More or less. All of those structures are now opaque. As Richard Ross III said in the comments you must use the introspection functions to get at the contents of these structures. This allows Apple to change the implementation details without requiring everything to be recompiled.
这篇关于objc_msgSend()分配表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!