类是 Objective-C 中的对象吗? [英] Are classes objects in Objective-C?

查看:34
本文介绍了类是 Objective-C 中的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

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

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

推荐答案

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

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

引用:

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

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

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

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.

元类呢?是吗元类一直向下?A号元类是根的一个实例类的元类;根元类本身是根的实例元类.isa 链以 a 结尾在这里循环:实例到类元类到根元类到它自己.元类 isa 指针的行为很少有关系,因为在现实中世界上没有人发送消息元类对象.[...]

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

更多有趣的阅读:

了解 Objective-C 运行时,作者:Colin Wheeler
(搜索标题为所以类定义对象..."的段落)

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

Objective-C 中的元类是什么? 作者:马特·加拉格

这篇关于类是 Objective-C 中的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆