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?
Objective-C Language Enums Defining an enumtypedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB = 5, MyEnumValueC = 10, }; You can also specify on the first value and all the following will use it with increment: typedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB, MyEnumValueC, };
A typedef in Objective-C is exactly the same as a typedef in C. And an enum in Objective-C is exactly the same as an enum in C. This declares an enum with three constants kCircle = 0, kRectangle = 1 and kOblateSpheroid = 2, and gives the enum type the name ShapeType.
Event though Swift supports string enums they cannot be used in Objective-C, only Int enums are allowed.
The typedef function is used to assign the new name to the datatype. So that you can use a custom name for predefined long names of the datatypes.
Because this new way helps you with autocompletion, switch statement, better, respectively more precise warnings, ...
Stick with macro ...
typedef NS_ENUM( NSUInteger, CarType ) { FourDoorCarType, TwoDoorCarType };
... read this for example https://stackoverflow.com/a/3190470/581190
NSInteger, ... what types do you want?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With