How can compiler create enum value for you?


#1

At the end of the enum section, the author said “You can leave out the values, and the compiler will make up values for you”, what does this means ?


#2

It means that the following two definitions of Code are equivalent.

enum Code {Code1, Code2, Code3, Code4, Code5};
enum Code {Code1 = 0, Code2 = 1, Code3 = 2, Code4 = 3, Code5 = 4};

If you don’t explicitly give values to any of the symbols, they will be sequentially assigned values starting from zero.


#3

[quote=“ibex10”]It means that the following two definitions of Code are equivalent.

enum Code {Code1, Code2, Code3, Code4, Code5};
enum Code {Code1 = 0, Code2 = 1, Code3 = 2, Code4 = 3, Code5 = 4};

If you don’t explicitly give values to any of the symbols, they will be sequentially assigned values starting from zero.[/quote]

Can’t imaging…
and Thanks, you’ve answer my question for the second time, it seems you’re an very experienced Objective-C programmer…


#4

Thanks for clearing that up. The book doesn’t specifically state that it will start the values at 0 and increase them incrementally by 1.

I knew that it couldn’t automatically assign the values you wanted it to (unless my Macbook is sentient), but I didn’t know what values it would assign.