The new enum in Objective-C
cocoa, objective-c
Solution
This explicitly defines the numeric type that the enum uses. This helps with portability concerns. It also helps if you need control over signed vs. unsigned
Two types are actually defined here. The first is `enum CarType` and the second is `CarType`, which is an alias of `enum CarType`. You can omit the first `CarType` if you want. This just prevents `enum CarType` from being a defined type, but `CarType` is still valid. Another common thing people do is something like
typedef enum _EnumName {
values
} EnumName;
What you decide to do here is something of a matter of personal preference.
Yes. You can use any numeric type, although the enum values must be able to fit in the chosen type.
Problem
In the latest tools, a new kind of `enum`s are now allowed: ``` typedef enum CarType : NSUInteger { FourDoorCarType, TwoDoorCarType } CarType; ``` My question comes in parts: Why should I use this instead of the old way? Why does `CarType` appear twice? I tried skipping the first `CarType` and just leaving the first line as "`typedef enum : NSUInteger {`", and it seems to work fine. What are the drawbacks, if any? Can some types other than `NSUInteger` be used?