Using the typedef and enum keywords we can define a type that can have either one value or another. It's one of the most important uses of the typedef keyword. This is the syntax of an enumerated type: typedef enum { //...
enum defines a type name automatically, While in case of typedef we define a new data type which may be of any kind of data type so that we do not have declare it explicitly everytime.
An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used. enum season { spring, summer, autumn, winter }; Here, the name of the enumeration is season .
The C standard specifies that enums are integers, but it does not specify the size. Once again, that is up to the people who write the compiler. On an 8-bit processor, enums can be 16-bits wide. On a 32-bit processor they can be 32-bits wide or more or less.
In one header file, I have something like:
// PasscodeInputViewController.h typedef enum { PasscodeInputModeOn, // set passcode PasscodeInputModeEnter, // enter passcode PasscodeInputModeChange, // change passcode PasscodeInputModeOff // turn off passcode } PasscodeInputMode;
In another header file, I declare a method that takes an argument of type PasscodeInputMode
:
#import "PasscodeInputViewController.h" - (void)presentPasscodeInputWithMode:(PasscodeInputMode)mode;
As you can see, I use #import "PasscodeInputViewController.h"
as above so that PasscodeInputMode
is recognized, but is there a @class
equivalent for typedef enum
?
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