本文为大地瓜原创,欢迎知识共享,转载请注明出处。
虽然你不注明出处我也没什么精力和你计较。
作者微信号:christgreenlaw
本文的原文。本文只对其进行翻译。
Objective-C is a class-based object system. Each object is an instance of some class; the object'sisa
pointer points to its class. That class describes the object's data: allocation size and ivar types and layout. The class also describes the object's behavior: the selectors it responds to and instance methods it implements.
Objective-C是一个基于class的对象系统。每个对象都是某个类的实例,对象的isa
指针指向其类。那个类描述了对象的数据:占用空间大小、ivar类型以及样式。这个类也描述了对象的行为:对象能够响应的selector以及其实现的实例方法。
The class's method list is the set of instance methods, the selectors that the object responds to. When you send a message to an instance,objc_msgSend()
looks through the method list of that object's class (and superclasses, if any) to decide what method to call.
类的方法列表是实例方法(也就是对象能够响应的selector)的集合。当你给你一个实例发送消息时,objc_msgSend()
查找对象的类(以及超类,如果有超类的话)的方法列表,以决定调用哪个方法。
Each Objective-C class is also an object. It has anisa
pointer and other data, and can respond to selectors. When you call a "class method" like[NSObject alloc]
, you are actually sending a message to that class object.
每一个Objective-C类也是一个对象。它有一个isa
指针以及其他的一些数据,可以响应selector。当你调用一个“类方法”时,比如[NSObject alloc]
,你实际上是在向类对象发送消息。
Since a class is an object, it must be an instance of some other class: a metaclass. The metaclass is the description of the class object, just like the class is the description of ordinary instances. In particular, the metaclass's method list is the class methods: the selectors that the class object responds to. When you send a message to a class - an instance of a metaclass -objc_msgSend()
looks through the method list of the metaclass (and its superclasses, if any) to decide what method to call. Class methods are described by the metaclass on behalf of the class object, just like instance methods are described by the class on behalf of the instance objects.
既然类是一个对象,那么它一定是某个其他类的实例:也就是元类(metaclass)。元类是类对象的一种描述,正如:类是普通实例的描述。需要指明的是,元类的方法列表是类方法,也就是类能够响应的selector。当你向一个类(元类的实例)发送消息时,objc_msgSend()
查找元类(以及元类的超类,如果元类有超类的话)的方法列表以决定执行哪一个方法。类方法是元类代表类对象所描述的,正如:实例方法是类代表对象所描述的。
What about the metaclass? Is it metaclasses all the way down? No. A metaclass is an instance of the root class's metaclass; the root metaclass is itself an instance of the root metaclass. Theisa
chain ends in a cycle here: instance to class to metaclass to root metaclass to itself. The behavior of metaclassisa
pointers rarely matters, since in the real world nobody sends messages to metaclass objects.
那元类呢?是不是元类还有元类,然后还有元类这样下去?并不是的。元类是一个根类的元类的实例。isa
链在此处成环结束:实例指向类指向元类指向根元类指向自己(根元类)。元类isa
指针的实际行为不太重要,因为实际操作中不会有人向元类对象发送消息。
More important is the superclass of a metaclass. The metaclass's superclass chain parallels the class's superclass chain, so class methods are inherited in parallel with instance methods. And the root metaclass's superclass is the root class, so each class object responds to the root class's instance methods. In the end, a class object is an instance of (a subclass of) the root class, just like any other object.
元类的超类更为重要。元类的超类链和类的超类链是平行的,所以类方法的继承关系和实例方法的继承关系也是平行的。根元类的超类就是根类,所以每个类对象都响应根类的实例方法。最后,类对象是根类的实例(或者说子类),就和其他对象一样。
Confused? The diagram may help. Remember, when a message is sent to any object, the method lookup starts with that object'sisa
pointer, then continues up the superclass chain. "Instance methods" are defined by the class, and "class methods" are defined by the metaclass plus the root (non-meta) class.
迷糊了吗????上面那个图应该能帮你理解。要记住的是,当一个消息发送给任何一个对象,方法查找都从这个对象的isa
指针开始,然后沿着超类链继续下去。“实例方法”是类定义的,“类方法”是元类和根类(注意不是根元类)定义的。
In proper computer science language theory, a class and metaclass hierarchy can be more free-form, with deeper metaclass chains and multiple classes instantiated from any single metaclass. Objective-C uses metaclasses for practical goals like class methods, but otherwise tends to hide metaclasses. For example,[NSObject class]
is identical to[NSObject self]
, even though in formal terms it ought to return the metaclass thatNSObject->isa
points to. The Objective-C language is a set of practical compromises; here it limits the class schema before it gets too, well, meta.
用计算机科学语言的理论来讲,类和元类的层级关系可以更自由,有更深层次的元类链,有从某一个元类实例化出来的多个类。Objective-C使用元类是为了达到更实际的目的,比如类方法,却又希望隐藏元类(的概念)。比如说,[NSObject class]
和[NSObject self]
其实是一模一样的,尽管用更正式的术语来说,[NSObject class]
应该返回NSObject->isa
所指向的元类。Objective-C是为了实践所折中的结果,它为了不变得过于meta而限制了类的schema。