gpt4 book ai didi

objective-c - 关于 obj-c 中的 "Class"类?

转载 作者:行者123 更新时间:2023-12-02 21:50:42 25 4
gpt4 key购买 nike

请遵守此方法声明:

NSString *NSStringFromClass(Class aClass);

我想了解 Class类,但我找不到有关它的文档,因为我不知道如何创建一个足够好的正则表达式并用它进行谷歌搜索。事实上,类名是 Class而不是NSClass让我思考Class不是一个真正的类(如 java.lang.Class )。但是,我不知道,所以我想研究一下。但是,我找不到文档。

如果我需要问一个具体问题:obj-c 中的“Class”是一个真正的类吗?它的方法和属性是什么?

最佳答案

引用自Cocoa with Love

Every object has a class. This is a fundamental object-oriented concept but in Objective-C, it is also a fundamental part of the data. Any data structure which has a pointer to a class in the right location can be treated as an object.In Objective-C, an object's class is determined by its isa pointer. The isa pointer points to the object's Class.

作为证明,这里将 id 声明为指向 objc_object 结构的指针。

typedef struct objc_object {
Class isa;
} *id;

那么我们就进入正题了。什么是

让我们看看定义

Class 定义如下(实际上可能会根据运行时的不同而有所不同,但让我们保持简单)

struct objc_class {
Class isa;
}
typedef struct objc_class *Class;

如您所见,Class 也有一个 isa 指针。它看起来很像 objc_object 定义,原因很简单:Class 实际上是一个对象。

但是Class的类是什么?根据定义,它是一个元类。

根据相同的来源(直接解决您的问题的部分以粗体显示),

The meta-class, like the Class before it, is also an object. This means that you can invoke methods on it too. Naturally, this means that it must also have a class.

All meta-classes use the base class' meta-class (the meta-class of the top Class in their inheritance hierarchy) as their class. This means that for all classes that descend from NSObject (most classes), the meta-class has the NSObject meta-class as its class.

Following the rule that all meta-classes use the base class' meta-class as their class, any base meta-classes will be its own class (their isa pointer points to themselves). This means that the isa pointer on the NSObject meta-class points to itself (it is an instance of itself).

要进一步阅读有关该主题的内容,请参阅另一个 great explanation作者:格雷格·帕克。

关于objective-c - 关于 obj-c 中的 "Class"类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18677030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com