views:

982

answers:

3

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.

+1  A: 

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};
Daniel Earwicker
These are all helpful answers. This idea of declaring a variable of the enumerated type is intriguing. Doing so, I suppose, limits the possible values of the variable. I have never used an enum like that in other languages. I just used the enum declaration itself as a holder of possible values. For example: If MyFish = FishSpecies.Mackeral Then...or, as I type, I can get IntelliSense as follows: x = FishSpecies.Cod Hallibut Mackeraletc. (Sorry I can't format this in a comment.) Can Xcode do that kind of IntelliSense with an enum, like VB?
Scott Pendleton
@Scott - the problem is that in C the possible enum value names are merged into the global scope (or in C++, they go into the same namespace the enum type is declared in). So in fact `FishSpecies` doesn't contain anything called `Cod`. You just have to say `Cod`. In C++ its good practice to always declare enums nested in some class or namespace to avoid pollution of the global namespace. In C++1x there will be an enhanced enum variety where you'd write `FishSpecies::Cod`. And of course in C#, VB.NET (and Java 5 or later I think) you already get what you're familiar with.
Daniel Earwicker
I'm pretty sure Objective C stays faithful to plain ANSI C aside from the dynamic OO extensions which are very much segregated from the rest of the language. Totally different approach from C++ which aims also to be "a better C". Though the advantage is that you can combine the two languages and get Objective C++ (which is what I'd use if I dabbled in iPhone dev, as there's no GC on the iPhone, and C++ enables an essential workaround called smart pointers... look them up!)
Daniel Earwicker
A: 

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?

Carl Norum
+1  A: 

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.

Arkku