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

IphoneObjective CC

Iphone Problem Overview


I declared an enum in my implementation file as shown below, and declared a variable of that type in my interface as PlayerState thePlayerState; and used the variable in my methods. But I am getting errors stating that it is undeclared. How do I correctly declare and use a variable of type PlayerState in my methods?:

In the .m file

@implementation View1Controller

    typedef enum playerStateTypes
    	{
    		PLAYER_OFF,
    		PLAYER_PLAYING,
    		PLAYER_PAUSED
    	} PlayerState;

in the .h file:

@interface View1Controller : UIViewController {
	
	PlayerState thePlayerState;

in some method in .m file:

-(void)doSomethin{

thePlayerState = PLAYER_OFF;

}

Iphone Solutions


Solution 1 - Iphone

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][1]

[1]: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html "Swift and CAPI's"

Solution 2 - Iphone

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.

Solution 3 - Iphone

In the .h:

typedef enum {
    PlayerStateOff,
    PlayerStatePlaying,
    PlayerStatePaused
} PlayerState;

Solution 4 - Iphone

With current projects you may want to use the NS_ENUM() or NS_OPTIONS() macros.

typedef NS_ENUM(NSUInteger, PlayerState) {
        PLAYER_OFF,
        PLAYER_PLAYING,
        PLAYER_PAUSED
    };

Solution 5 - Iphone

This is how Apple does it for classes like NSString:

In the header file:

enum {
    PlayerStateOff,
    PlayerStatePlaying,
    PlayerStatePaused
};

typedef NSInteger PlayerState;

Refer to Coding Guidelines at http://developer.apple.com/

Solution 6 - Iphone

I recommend using NS_OPTIONS or NS_ENUM. You can read more about it here: http://nshipster.com/ns_enum-ns_options/

Here's an example from my own code using NS_OPTIONS, I have an utility that sets a sublayer (CALayer) on a UIView's layer to create a border.

The h. file:

typedef NS_OPTIONS(NSUInteger, BSTCMBorder) {
    BSTCMBOrderNoBorder     = 0,
    BSTCMBorderTop          = 1 << 0,
    BSTCMBorderRight        = 1 << 1,
    BSTCMBorderBottom       = 1 << 2,
    BSTCMBOrderLeft         = 1 << 3
};

@interface BSTCMBorderUtility : NSObject

+ (void)setBorderOnView:(UIView *)view
                 border:(BSTCMBorder)border
                  width:(CGFloat)width
                  color:(UIColor *)color;

@end

The .m file:

@implementation BSTCMBorderUtility

+ (void)setBorderOnView:(UIView *)view
                 border:(BSTCMBorder)border
                  width:(CGFloat)width
                  color:(UIColor *)color
{
    
    // Make a left border on the view
    if (border & BSTCMBOrderLeft) {
        
    }
    
    // Make a right border on the view
    if (border & BSTCMBorderRight) {
        
    }
    
    // Etc
    
}

@end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRexOnRoidsView Question on Stackoverflow
Solution 1 - IphonerebelzachView Answer on Stackoverflow
Solution 2 - IphoneDave DeLongView Answer on Stackoverflow
Solution 3 - IphoneBen FlynnView Answer on Stackoverflow
Solution 4 - Iphonesean woodwardView Answer on Stackoverflow
Solution 5 - IphoneSanthos RamalingamView Answer on Stackoverflow
Solution 6 - IphoneJohannesView Answer on Stackoverflow