gpt4 book ai didi

objective-c - 为什么 Objective-C 中的 id 类型会省略星号?

转载 作者:太空狗 更新时间:2023-10-30 03:41:18 24 4
gpt4 key购买 nike

我对 Objective-C id 类型的理解是它是指向未知类型对象的指针。在我看来,它就像是 Objective-C 类的 void* ,上面有一些编译器糖,用于类型检查之类的东西。我的问题是为什么不是 id* 而不是 id 因为它是指针类型?如果这是未使用 * 声明对象引用的 Java 或 C#,那么我就不会感到如此惊讶。但是在 Objective-C 中,每个类都由 C 风格的指针引用,那么为什么要隐藏未知对象类型的 * 呢?

最佳答案

id typedef 已经是一个指针:

typedef struct objc_object {
Class isa;
} *id;

来自Objective-C Programming Language :

In Objective-C, object identifiers are of a distinct data type: id. This type is the general type for any kind of object regardless of class and can be used for instances of a class and for class objects themselves.

id anObject;

For the object-oriented constructs of Objective-C, such as method return values, id replaces int as the default data type. (For strictly C constructs, such as function return values, int remains the default type.)

The keyword nil is defined as a null object, an id with a value of 0. id, nil, and the other basic types of Objective-C are defined in the header file objc/objc.h.

id is defined as pointer to an object data structure:

typedef struct objc_object {
Class isa;
} *id;

Every object thus has an isa variable that tells it of what class it is an instance. Since the Class type is itself defined as a pointer:

typedef struct objc_class *Class;

the isa variable is frequently referred to as the “isa pointer.”

关于objective-c - 为什么 Objective-C 中的 id 类型会省略星号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12639055/

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