Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
707 views
in Technique[技术] by (71.8m points)

objective-c - Objective-C中的typedef枚举是什么?(What is a typedef enum in Objective-C?)

I don't think I fundamentally understand what an enum is, and when to use it.

(我不认为我从根本上理解enum是什么,以及何时使用它。)

For example:

(例如:)

typedef enum {
    kCircle,
    kRectangle,
    kOblateSpheroid
} ShapeType;

What is really being declared here?

(这里真的被宣布了什么?)

  ask by Craig translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Three things are being declared here: an anonymous enumerated type is declared, ShapeType is being declared a typedef for that anonymous enumeration, and the three names kCircle , kRectangle , and kOblateSpheroid are being declared as integral constants.

(这里声明了三件事:声明了一个匿名枚举类型, ShapeType被声明为该匿名枚举的typedef,并且三个名称kCirclekRectanglekOblateSpheroid被声明为整数常量。)

Let's break that down.

(让我们打破它。)

In the simplest case, an enumeration can be declared as

(在最简单的情况下,枚举可以声明为)

enum tagname { ... };

This declares an enumeration with the tag tagname .

(这声明了带有标记tagname的枚举。)

In C and Objective-C (but not C++), any references to this must be preceded with the enum keyword.

(在C和Objective-C(但不是 C ++)中,对此的任何引用都必须enum关键字开头。)

For example:

(例如:)

enum tagname x;  // declare x of type 'enum tagname'
tagname x;  // ERROR in C/Objective-C, OK in C++

In order to avoid having to use the enum keyword everywhere, a typedef can be created:

(为了避免在任何地方使用enum关键字,可以创建一个typedef:)

enum tagname { ... };
typedef enum tagname tagname;  // declare 'tagname' as a typedef for 'enum tagname'

This can be simplified into one line:

(这可以简化为一行:)

typedef enum tagname { ... } tagname;  // declare both 'enum tagname' and 'tagname'

And finally, if we don't need to be able to use enum tagname with the enum keyword, we can make the enum anonymous and only declare it with the typedef name:

(最后,如果我们不需要能够将enum tagnameenum关键字一起使用,我们可以使enum匿名并仅使用typedef名称声明它:)

typedef enum { ... } tagname;

Now, in this case, we're declaring ShapeType to be a typedef'ed name of an anonymous enumeration.

(现在,在这种情况下,我们将ShapeType声明为匿名枚举的typedef名称。)

ShapeType is really just an integral type, and should only be used to declare variables which hold one of the values listed in the declaration (that is, one of kCircle , kRectangle , and kOblateSpheroid ).

(ShapeType实际上只是一个整数类型,并且只应用于声明包含声明中列出的值之一的变量(即kCirclekRectanglekOblateSpheroid )。)

You can assign a ShapeType variable another value by casting, though, so you have to be careful when reading enum values.

(但是,您可以通过强制转换为ShapeType变量指定另一个值,因此在读取枚举值时必须小心。)

Finally, kCircle , kRectangle , and kOblateSpheroid are declared as integral constants in the global namespace.

(最后, kCirclekRectanglekOblateSpheroid在全局命名空间中声明为整数常量。)

Since no specific values were specified, they get assigned to consecutive integers starting with 0, so kCircle is 0, kRectangle is 1, and kOblateSpheroid is 2.

(由于未指定特定值,因此将它们分配kCircle 0开头的连续整数,因此kCircle为0, kRectangle为1, kOblateSpheroid为2。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...