tags:

views:

276

answers:

2

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;

cheers gary

+4  A: 

good explanation, right here: http://stackoverflow.com/questions/707512/typedef-enum-in-objective-c

Oren Mazor
+2  A: 

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;
    .
    .
    .
}

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!

BitDrink