Objective C - 何时应该使用“typedef”在“enum”之前,什么时候应该命名枚举?

发布于 2024-08-27 21:59:39 字数 531 浏览 4 评论 0原文

在示例代码中,我看到了 this:

typedef enum Ename { Bob, Mary, John} EmployeeName;

和 this:

typedef enum {Bob, Mary, John} EmployeeName;

和 this:

typedef enum {Bob, Mary, John};

但对我来说成功编译的是:

enum {Bob, Mary, John};

我将该行放在 @interface 行上方的 .h 文件中,然后当我 #import 该 .h 文件到不同类的 .m 文件,那里的方法可以看到枚举。

那么,什么时候需要其他变体呢?

如果我可以将枚举命名为 EmployeeNames 之类的名称,然后当我键入“EmployeeNames”后跟“.”时,如果弹出一个列表显示枚举选项,那就太好了。

In sample code, I have seen this:

typedef enum Ename { Bob, Mary, John} EmployeeName;

and this:

typedef enum {Bob, Mary, John} EmployeeName;

and this:

typedef enum {Bob, Mary, John};

but what compiled successfully for me was this:

enum {Bob, Mary, John};

I put that line in a .h file above the @interface line, and then when I #import that .h file into a different class's .m file, methods there can see the enum.

So, when are the other variants needed?

If I could name the enum something like EmployeeNames, and then, when I type "EmployeeNames" followed by a ".", it would be nice if a list pops up showing what the enum choices are.

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

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

发布评论

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

评论(3

泅人 2024-09-03 21:59:39

在 C(以及 Objective C)中,每次使用枚举类型时都必须以 enum 为前缀。

enum MyEnum enumVar;

通过创建 typedef:

typedef MyEnum MyEnumT;

您可以编写更短的:

MyEnumT enumVar;

替代声明在一个声明中声明枚举本身和 typedef。

// gives the enum itself a name, as well as the typedef
typedef enum Ename { Bob, Mary, John} EmployeeName;

// leaves the enum anonymous, only gives a name to the typedef
typedef enum {Bob, Mary, John} EmployeeName;

// leaves both anonymous, so Bob, Mary and John are just names for values of an anonymous type
typedef enum {Bob, Mary, John};

In C (and hence Objective C), an enum type has to be prefixed with enum every time you use it.

enum MyEnum enumVar;

By making a typedef:

typedef MyEnum MyEnumT;

You can write the shorter:

MyEnumT enumVar;

The alternative declarations declare the enum itself and the typedef in one declaration.

// gives the enum itself a name, as well as the typedef
typedef enum Ename { Bob, Mary, John} EmployeeName;

// leaves the enum anonymous, only gives a name to the typedef
typedef enum {Bob, Mary, John} EmployeeName;

// leaves both anonymous, so Bob, Mary and John are just names for values of an anonymous type
typedef enum {Bob, Mary, John};
甜尕妞 2024-09-03 21:59:39

enum { } 中的名称定义枚举值。当您给它一个名称时,您可以将它与关键字enum一起用作类型,例如enum EmployeeName b = Bob;。如果您还typedef它,那么您可以在声明该类型的变量时删除enum,例如EmployeeName b = Bob;而不是前面的例子。

The names inside enum { } define the enumerated values. When you give it a name, you can use it as a type together with the keyword enum, e.g. enum EmployeeName b = Bob;. If you also typedef it, then you can drop the enum when you declare variables of that type, e.g. EmployeeName b = Bob; instead of the previous example.

美羊羊 2024-09-03 21:59:39

你的第三个例子与你的最后一个例子相同 - typedef 没有用 - GCC 甚至给出了关于这种情况的警告:

warning: useless storage class specifier in empty declaration

你的第一个和第二个例子也部分等效,因为它们都给出了枚举输入姓名EmployeeName。第一个示例还允许您将 enum EnameEmployeeName 互换使用;在第二个示例中,EmployeeName 是唯一的选项。第二个示例必须按照您的方式编写 - 您可以按如下方式分解第一个示例:

enum Ename { Bob, Mary, John };
typedef enum Ename EmployeeName;

也许这有助于澄清问题?

Your third example is the same as your last example - the typedef there is useless - GCC even gives a warning about that case:

warning: useless storage class specifier in empty declaration

Your first and second example are also partly equivalent, in that they both give the enum type a name EmployeeName. The first example also lets you use enum Ename interchangeably with EmployeeName; in the second example, EmployeeName is the only option. The second example has to be written as you have - you can decompose the first example as follows:

enum Ename { Bob, Mary, John };
typedef enum Ename EmployeeName;

Maybe that helps clear things up?

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