How do I define and use an ENUM in Objective-C? How do I define and use an ENUM in Objective-C? objective-c objective-c

How do I define and use an ENUM in Objective-C?


Apple provides a macro to help provide better code compatibility, including Swift. Using the macro looks like this.

typedef NS_ENUM(NSInteger, PlayerStateType) {  PlayerStateOff,  PlayerStatePlaying,  PlayerStatePaused};

Documented here


Your typedef needs to be in the header file (or some other file that's #imported into your header), because otherwise the compiler won't know what size to make the PlayerState ivar. Other than that, it looks ok to me.


In the .h:

typedef enum {    PlayerStateOff,    PlayerStatePlaying,    PlayerStatePaused} PlayerState;