关于macos:Objective-C中的类对象是什么?

Are classes objects in Objective-C?

好吧,我理解对象是一个必须分配和初始化的类的实例,但是类本身是对象吗?

我知道当你创建一个新的类时,它是其他东西的一个实例,比如nsObject。所以,如果这使它成为一个类,那么对象不仅可以包含变量和方法,还可以包含其他对象,对吗?

抱歉,这可能是非常基本的,但我正在读两本关于cocoa和xcode的书,这一点有点不清楚(可能是因为我在其他语言方面缺乏经验)。


这是格雷格·帕克对此事的一个很好的解释。

引用:

[...] Each Objective-C class is also an
object. It has an isa 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.

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.

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. The isa chain ends in a
cycle here: instance to class to
metaclass to root metaclass to itself.
The behavior of metaclass isa pointers
rarely matters, since in the real
world nobody sends messages to
metaclass objects. [...]

更有趣的是:

ColinWheeler对Objective-C运行时的理解(搜索标题为"so classes define objects…"的段落)

什么是Objective-C中的元类?马特·加拉赫