在Objective-C使用枚举? [英] Using enum in Objective-C?
问题描述
这是正确的(甚至是一个有效的方式)在Objective-C使用emums?即不使用菜单项,但只是定义了一个列表中添加= 1,负载= 2,表= 3等。
Is this the correct (or even a valid way) to use emums in Objective-C? i.e. The menuItem is not used but just defines a list add=1, load=2, list=3 etc.
enum menuItems {
add = 1 ,
save ,
load ,
list ,
removeAll ,
remove ,
quit
};
int optionSelect;
scanf("%d", &optionSelect);
switch (optionSelect) {
case add:
//...
break;
}
欢呼加里
推荐答案
如果你想给一个语义的枚举,你可以考虑定义一个定制的类型和声明变量optionSelect作为该类型的变量!
在code ...
If you want to give a semantic meaning to the enumeration, you can consider to define a customized type and declare the variable "optionSelect" as variable of that type! In code...
typedef enum menuItems {
add = 1,
save,
load,
list,
removeAll,
remove,
quit} MenuItem;
MenuItem optionSelect;
scanf("%d", &optionSelect);
switch (optionSelect) {
case add:
...
break;
.
.
.
}
这是差不多,你已经写了,但是从开发商的侧面你给一个特定意义的变量optionSelect同样的事情,而不仅仅是一个简单的INT!
That is, almost, the same thing you have written, but from the side of the developer you give a particular meaning to the variable "optionSelect", not just a simple int!
这篇关于在Objective-C使用枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!