Objective C Enum: How to Declare and Use Enumerated Types in Objective C

objective c enumObjective C has been around for a long, long time. It was developed in the 1980s, when a couple of developers felt the need to add object oriented capabilities to C. Objective C is the language used by Apple to make its iOS systems. It is the language that is used to build applications on the Cocoa API.  While Objective C did not go on to become as popular as some of the other modern computer languages (C++ or Java), the language is a favorite with app developers. Objective C has a simple syntax that is easy to understand and remember. It lets you use programs written for C with little to no modifications. You can just pick up whole programs and even apps written in C and use them with Objective C.

Objective C is an object-oriented programming language. It is powerful, flexible and can be used to develop outstanding apps. You will find it easy to learn Objective C if you have learned C or C++ before. If you’re a beginner, you will have to get a good grounding on the basics to pick it up. We have written several tutorials on Objective C- you can refer to them to get started with learning it. We also offer an introductory course on Objective C, in which we cover all the important basic concepts of the language.

In this tutorial, we’re going to give you an overview of the enumerated types in Objective C. You won’t have a problem understanding this tutorial if you’re familiar with C or Objective C basics like variables and type declarations.

What are Enumerated Types in Objective C?

An enumerator is a list of predefined variables. If you’re familiar with Objective C data types like int and double, you know that you can define variables to hold or return int and double values. Enumerators are also a data type – the only different is that you create the data type. With enumerators, you can create variables that will hold only a value found only in the enumerator. Enumerators are declared with the help of the “enum” keyword.

The syntax to create an enumerated data type is as follows:

enum name_of_enumerator (element 1, element 2, element 3…);

After you’re done creating an enumerator, you can declare a variable that uses the enumerator:

enum name_of_enumerator name_of_variable;

In contrast, the integer data type contains a list of numbers. When you create a variable to reference to an int, it can only accept values found in the integer data type:

int a = 5;

Here, a could have taken any value found in the integer data type, but it couldn’t have taken a value outside it (like a decimal or a fraction value). Similarly, a variable that is declared with the enum type will not be able to take a value outside the predefined enumerator.

To learn more about data types in Objective C, you can refer to our Objective C tutorials. You can also sign up for this Objective C course.

Examples of the Enumerator Type

So what is the use of an enumerator type? If you were designing a calendar app, for example, you could use an enumerator type to store the values of the 12 months:

enum months {Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sept, Oct, Nov, Dec};

Here, we’ve created an enumerator type called “months”. It is a collection of 12 elements. If you create a new variable and declare it to be of the “months” type, it will have to take one of the 12 possible values:

enum months {Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sept, Oct, Nov, Dec};
enum months currentMonth;
currentMonth = June;

Here, we created a variable called “currentMonth” and declared it to be of type “months”. Then, we assigned it a value “June”.

What would happen if you tried to give your variable a value that doesn’t exist in the pre-declared enumerator?

enum months {Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sept, Oct, Nov, Dec};
enum months currentMonth;
currentMonth = OctoJan;

Output:

OctoJan undeclared

Because “OctoJan” wasn’t declared a part of the months enumerator, we received an OctoJan undeclared error message. You can learn how to write your own Objective C programs with enums in this course.

Unique Integer Value

Every element in an enumerator is assigned a unique integer value (much like an array). The first element has a value of 0, the second of 1 and so on. This is the default assignment and it can be changed. Take out “months” enumerator for an example:

enum months {Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sept, Oct, Nov, Dec};

Here, Jan corresponds to 0, Mar to 2 and Dec to 11. You can change this default scheme if you want:

enum months {Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sept, Oct = 19, Nov =22, Dec};

Here, the element Oct will be assigned a value of 19, while Nov will be 22. What will the value of Dec be? You may say 11, but it’s actually now 23. When you assigned the value 22 to the element preceding Dec (Nov), Dec automatically got assigned the value 23. The rest of the elements remain unchanged.

You are allowed to set both positive as well as negative values.

Let’s write a final example program:

enum weight {slim = 120, medium= 140, stocky = 160};
enum weight currentWeight;
int currentWeight = medium;
NSLog (@"The Current Weight is %i pounds", currentWeight);

Output:

The Current Weight is 140 pounds.

We created an enumerator that has 3 elements, and then we created a variable called “currentWeight” to store one of those elements. We declared it an “int” data type, used it to reference the “medium” element and then finally we printed the value of the variable- and we received the expected output of 140.

The enumerator data type is very useful when you’re writing large programs. It improved readability and helps you avoid mistakes. To learn how to write your own iPhone apps with Objective C, try out this course.