Objective-C中的typedef枚举是什么? [英] What is a typedef enum in Objective-C?
问题描述
我不认为我从根本上了解一个枚举
是什么时候使用它。
例如:
typedef枚举{
kCircle ,
kRectangle,
kOblateSpheroid
} ShapeType;
真正在这里声明什么?
这里声明了三件事:一个匿名枚举类型被声明为 ShapeType
被声明为该匿名枚举的typedef,三个名字 kCircle
, kRectangle
和 kOblateSpheroid
正在声明为积分常数。
让我们把它打破。在最简单的情况下,枚举可以被声明为
枚举标记名{...};
这个声明枚举包含标签 tagname
。在C和Objective-C(但不是 C ++)中,对的任何引用必须之前是枚举
关键字。例如:
枚举标记名x; //声明x类型'枚举标记名'
标记名x; // C / Objective-C中的错误,C ++中的OK
为了避免使用枚举
关键字无处不在,可以创建一个typedef:
枚举标记名{ ...}
typedef枚举标记名标记名; //将'tagname'声明为'enum tagname'的typedef
这可以简化为一行:
typedef枚举标记名{...}标记名; //声明枚举标记名和标记名
最后,如果我们不需要为了能够使用枚举
关键字枚举标记名
,我们可以使枚举
匿名,只用typedef名称声明:
typedef枚举{...}标记名;
现在,在这种情况下,我们声明了 ShapeType
是一个匿名枚举的typedef的名称。 ShapeType
真的只是一个整体类型,只能用于声明保存声明中列出的值之一的变量(即 kCircle
, kRectangle
和 kOblateSpheroid
)。您可以通过转换指定一个 ShapeType
变量另一个值,所以在读取枚举值时必须小心。
<最后,
kCircle
, kRectangle
和 kOblateSpheroid
在全局命名空间中声明为整数常量。由于没有指定特定的值,它们被分配到从0开始的连续整数,所以 kCircle
是0, kRectangle
1,而 kOblateSpheroid
是2。 I don't think I fundamentally understand what an enum
is, and when to use it.
For example:
typedef enum {
kCircle,
kRectangle,
kOblateSpheroid
} ShapeType;
What is really being declared here?
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.
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
. In C and Objective-C (but not C++), any references to this must be preceded with the enum
keyword. 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 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:
typedef enum { ... } tagname;
Now, in this case, we're declaring ShapeType
to be a typedef'ed name of an anonymous enumeration. 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
). You can assign a ShapeType
variable another value by casting, though, so you have to be careful when reading enum values.
Finally, kCircle
, kRectangle
, and kOblateSpheroid
are declared as integral constants in the global namespace. 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.
这篇关于Objective-C中的typedef枚举是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!