Objective-C 中的类是对象吗?

发布于 2024-11-03 03:56:37 字数 204 浏览 0 评论 0原文

好的,所以我明白对象是必须分配和初始化的类的实例,但是类本身是对象吗?

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

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紅太極 2024-11-10 03:56:37

这里是 Greg Parker 引用的对此事的一个很好的解释

[...] 每个 Objective-C 类也是一个
目的。它有一个 isa 指针和
其他数据,并可以响应
选择器。当你调用“类”时
方法”,比如 [NSObject alloc],你是
实际上向其发送消息
类对象。

既然类是一个对象,那么它必须是
其他类的实例:a
元类。元类是
类对象的描述,只是
就像类的描述一样
普通实例。特别是,
元类的方法列表就是类
方法:类的选择器
对象响应。当您发送一个
发送给类的消息 - 的实例
元类 - objc_msgSend() 看起来
通过方法列表
元类(及其超类,如果
any) 来决定调用什么方法。
类方法由以下描述
元类代表类
对象,就像实例方法一样
由类代表描述
实例对象。

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

更多有趣的读物:

了解 Objective-C 运行时科林·惠勒
(搜索标题为“因此类定义对象...”的段落)

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

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.

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

Further interesting reads:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文