gpt4 book ai didi

objective-c - 是否有与 C++ 的类范围枚举等效的东西?

转载 作者:行者123 更新时间:2023-12-03 16:32:30 25 4
gpt4 key购买 nike

在 C++ 中,我可能会在 header 中执行以下操作:

cClass {
enum eList { FIRST, SECOND };
}

...这在其他类(class)中:

cClass::eList ListValue = GetListValue();
if(ListValue == cClass::FIRST) {
...
}

是否有使用直接 Objective-C 语言功能或 Cocoa 中的一些技巧的等效方法,可以允许类似的作用域 枚举

最佳答案

嗯,您可以使用 C: 来模拟它的部分内容

创建一个 C 枚举并输入:

enum MONEnumType : uint8_t {
MONEnumType_Undefined = 0,
MONEnumType_Red,
MONEnumType_Green,
MONEnumType_Blue
};

声明容器:

struct MONEnum {
const enum MONEnumType Red, Green, Blue;
};

声明存储:

extern const struct MONEnum MONEnum;

定义存储:

const struct MONEnum MONEnum = {
.Red = MONEnumType_Red,
.Green = MONEnumType_Green,
.Blue = MONEnumType_Blue
};

使用中:

enum MONEnumType ListValue = GetListValue();
if (ListValue == MONEnum.Red) {
/* ... */
}

关于objective-c - 是否有与 C++ 的类范围枚举等效的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12973999/

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