Objective-C:类似于 Java 的枚举(每个枚举都有 int 值和许多其他值)

发布于 2024-10-10 13:59:53 字数 686 浏览 3 评论 0原文

在 Java 中,您可以创建一个具有多个值的枚举。 在 Objective-C 中,这并不容易做到。 我已经阅读了很多有关此内容的页面,但没有找到任何令人满意的内容可以让我通过简单的方式使用枚举并将枚​​举声明及其不同值保留在同一文件中

我想在 enums.h 中编写类似的内容:

// ========================================
typedef enum {eRED, eGREEN, eBLUE} ColorEnum;
int colorValues[] = { 0xFF0000, 0x00FF00, 0x0000FF };
NSArray *colorNames = [NSArray arrayWithObjects:@"Red color", @"light green", @"Deep blue", nil];
// ========================================

并能够使用这些全局变量来管理我的东西,例如:

int color = colorValues[eRED];

但我不知道如何编写它。 我有编译错误,例如“ColorValues”被定义了很多次。 或者,如果我只使用“静态”,我有许多“ColorValues”未在 .m 文件中使用...

你能帮助我吗?

In Java, you can make an enum having multiples values.
In objective-C, this cannot be done easily.
I've read many pages about this but I didn't found anything satisfying that would allow me to use enums by a simple way and to keep the enum declaration and their different values in the same file.

I would like to write something like this in a enums.h :

// ========================================
typedef enum {eRED, eGREEN, eBLUE} ColorEnum;
int colorValues[] = { 0xFF0000, 0x00FF00, 0x0000FF };
NSArray *colorNames = [NSArray arrayWithObjects:@"Red color", @"light green", @"Deep blue", nil];
// ========================================

and be able to use thoses global variables to manage my stuff anywhere like :

int color = colorValues[eRED];

But I don't know how to write this.
I have compile errors like "ColorValues" is defines many times.
Or if I just use "static", I have many "ColorValues" not used in .m file...

Could you help me ?

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

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

发布评论

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

评论(3

无言温柔 2024-10-17 13:59:53

你已经很接近了 - 问题是你将数组的定义放在头文件中,多个编译单元最终会复制它。将中间行:

int colorValues[] = { 0xFF0000, 0x00FF00, 0x0000FF };

放入 .m 文件之一,并将标题更改为:

extern int colourValues[];

您需要对 colorNames 执行类似的操作。将标头更改为:

extern NSArray *colorNames;

然后声明实际对象并在 .m 文件中对其进行初始化。

You're close - the problem is that you put the definition of your array in the header file, where multiple compilation units end up duplicating it. Put your middle line:

int colorValues[] = { 0xFF0000, 0x00FF00, 0x0000FF };

into one of your .m files, and change the header to read:

extern int colourValues[];

You'll need to do something similar with colorNames. Change the header to:

extern NSArray *colorNames;

And then declare the actual object and initialize it in a .m file.

就像说晚安 2024-10-17 13:59:53

你的问题与枚举无关。问题在于在标头中定义变量(而不仅仅是声明它们)。只需在标头中声明它们并将实现移至实现文件即可。

另外,您不能在方法之外编写 [NSArray arrayWithObjects...] 。那里只允许静态初始值设定项(即可以在编译时确定的值),而此类消息只能在运行时解析。解决方案是将赋值移至初始化函数中(例如,对于单例而言init,对于类而言initialize)。

Your problem has nothing to do with the enum. It's defining variables in a header (rather than just declaring them) that's the problem. Just declare them in the header and move the implementation to an implementation file.

Also, you can't write [NSArray arrayWithObjects…] outside of a method. Only static initializers (that is, values that can be determined at compile time) are allowed there, whereas messages like that are only resolved at runtime. The solution is to move the assignment into an initialization function (e.g. init for a singleton or initialize for a class).

↘人皮目录ツ 2024-10-17 13:59:53

我尝试了下面的方法,效果非常好:

in enums.h

@interface enums
    // here, the enum and its values are in the same place.

    typedef enum {eRED, eGREEN, eBLUE} ColorEnum;
    #define kColourValue  { 0xFF0000, 0x00FF00, 0x0000FF }
    extern int colourValues[];
@end

in enums.m

@implementation enums
    int colorValues[] = kColourValue;
@end

你在 Objective-C 中得到了一个完美的 Java 风格的枚举实现。
哇,两周的搜索......

I tryed the following thing that works like a charm :

in enums.h

@interface enums
    // here, the enum and its values are in the same place.

    typedef enum {eRED, eGREEN, eBLUE} ColorEnum;
    #define kColourValue  { 0xFF0000, 0x00FF00, 0x0000FF }
    extern int colourValues[];
@end

in enums.m

@implementation enums
    int colorValues[] = kColourValue;
@end

And you have a perfect Java style implementation of enums in Objective-C.
Wow, two weeks of searches....

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