Objective-C中的类对象是什么?

时间:2022-09-07 19:24:43

okay, so I understand that an object is an instance of a class that must be allocated and initialized, but are classes themselves objects?

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

I know when you create a new class it is an instance of something else, like NSObject. So, if this makes it a class, then objects can hold not only variables and methods, but other objects as well, right?

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

Sorry, this is probably really basic, but I am reading two books about cocoa and xcode and this point is a little unclear (probably because of my lack of experience in other languages).

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

1 个解决方案

#1


31  

Here is a pretty good explanation of the matter by Greg Parker

这是格雷格帕克对此事的一个非常好的解释

Quoting:

引用:

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

[...]每个Objective-C类也是一个对象。它有一个isa指针和其他数据,可以响应选择器。当您调用[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.

由于类是一个对象,它必须是某个其他类的实例:一个元类。元类是类对象的描述,就像类是普通实例的描述一样。特别是,元类的方法列表是类方法:类对象响应的选择器。当您向类(一个元类的实例)发送消息时,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. 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. [...]

元类怎么样?这是元类吗?不。元类是根类的元类的实例;根元类本身就是根元类的一个实例。 isa链在这里以一个循环结束:实例到类到元类到根元类到它自己。元类的行为很少很重要,因为在现实世界中没有人向元类对象发送消息。 [...]

Further interesting reads:

更有趣的读物:

Understanding the Objective-C Runtime by Colin Wheeler
(search for paragraph titled "So Classes define objects…")

了解Colin Wheeler的Objective-C运行时(搜索标题为“So Classes define objects ...”的段落)

What is a meta-class in Objective-C? by Matt Gallagher

Objective-C中的元类是什么?作者:Matt Gallagher

#1


31  

Here is a pretty good explanation of the matter by Greg Parker

这是格雷格帕克对此事的一个非常好的解释

Quoting:

引用:

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

[...]每个Objective-C类也是一个对象。它有一个isa指针和其他数据,可以响应选择器。当您调用[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.

由于类是一个对象,它必须是某个其他类的实例:一个元类。元类是类对象的描述,就像类是普通实例的描述一样。特别是,元类的方法列表是类方法:类对象响应的选择器。当您向类(一个元类的实例)发送消息时,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. 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. [...]

元类怎么样?这是元类吗?不。元类是根类的元类的实例;根元类本身就是根元类的一个实例。 isa链在这里以一个循环结束:实例到类到元类到根元类到它自己。元类的行为很少很重要,因为在现实世界中没有人向元类对象发送消息。 [...]

Further interesting reads:

更有趣的读物:

Understanding the Objective-C Runtime by Colin Wheeler
(search for paragraph titled "So Classes define objects…")

了解Colin Wheeler的Objective-C运行时(搜索标题为“So Classes define objects ...”的段落)

What is a meta-class in Objective-C? by Matt Gallagher

Objective-C中的元类是什么?作者:Matt Gallagher