C# to Objective-C - Part 5 : Enums

Stephen Zaharuk / Monday, November 26, 2012

Today’s post is actually going to be a really short one. There isn’t too much to really show, however its something that I had to look up a few times when I started working with Objective-C, so I figured its a good topic to cover anyways.

C#:
public enum Orientation
{
   Vertical = 0,
   Horizontal
}

Objective-C:
typedef enum
{
   OrientationVertical = 0,
   OrientationHorizontal
}Orientation;

As in C#, it is optional to set the integer value of an enumeration object. 

You should include your enum declaration in a header (.h) file as opposed to the implementation(.m) file.

Now lets take a look at consuming the enumeration we just created. 

C#:
public Orientation LabelOrientation{get;set;};

this.LabelOrientation = Orientation.Horizontal;

Objective-C:
@property(nonatomic, assign) Orientation labelOrientation;

self.labelOrientation = OrientationHorizontal;

Note the differences. When you access an enum value in C#, you use dot notation to obtain a value. Where as in Objective-C you use the value straight up. Which is why its good practice to include the enumeration's name in each of your enum values.

And thats a quick look at Enumerations in Objective-C.

By Stephen Zaharuk (SteveZ)