Objective-C Lesson 7: Enumerated Types and typedefs

Aside from the built-in data types, such as ints and floats, as well as the types provided by the Foundation and Cocoa classes, such as NSArray, you can create your own types—to an extent.

Enumerated Types

Objective-C allows you to create a type that will only take certain, predefined values. This is particularly useful, as it allows the programmer to model real-world collections, such as the months of the year, or the players in a game with a specific number of players. The syntax is quite simple. To create a Direction type as might be found in a map application, use the following code:

enum direction {north, south, west, east};

You can then use it anywhere else you might have a variable (prefixing it with the enum keyword):

enum direction currentDirection = south;
     // …
     if (currentDirection == north)
          NSLog(@"Player is going north.");
     // …

Note that simply declaring an enumerated type does not result in compiler enforcement—in the previous example, any value could have been assigned to currentDirection and the compiler would not have complained. But this, obviously, defeats the purpose of the enumerated type in the first place.

Note that an enumerated type is simply a specific list of integers, where each value is given one (or more) names. By default, the first element in a list is set equal to 0, and each subsequent element is assigned a value one greater. In the example above, the value north is equivalent (in all contexts) to the integral constant 0, south is equal to 1, and so on. You can also specify an integer value to be equal to an enumerated value:

enum direction {north, south = 5, west = 4, east};

Here, north is equal to 0, south is equal to 5, west is equal to 4, and because no value has been specified for east, it uses the “next” value after the previous declaration—therefore, east is equivalent to 5. This is perfectly acceptable—in one enum, multiple values can share one representative value.

Note also that the defined values do not have to be positive. In Foundation, there is an enumerated type is (partly—the rest is below) defined as:

enum {
   NSOrderedAscending = -1,
};     //…

The purpose of enumerated types is to create specifically named constants—don’t think of them as integers. Think of them as explicit names for certain values.

The typedef Statement

A typedef allows the programmer to define one Objective-C type as another. For example,

typedef int Counter;

defines the type Counter to be equivalent to the int type. This drastically improves code readability.

It is also useful if you have some simple code from another language, such as Java:

typedef NSString *String;
     // Java string
     String userInput = //… 

     // Valid Objective-C code because of previous typedef
     // (String userInput) is the same as (NSString *userInput)
     // And will be treated by the compiler as such

To define a new typedef,

  1. Write the statement as if a variable of the original type was being declared
  2. Where you would put the name of the variable (after the type), place the new type name.
  3. Prefix everything with typedef

This is particularly useful with enumerated types. To create a new type called Direction, we could write

typedef enum {north, south, west, east} Direction;
     Direction initialStep = // …

The NSComparisonResult is defined in a similar manner. The full definition from Apple’s website:

enum {
   NSOrderedAscending = -1,
typedef NSInteger NSComparisonResult;

Many comparison methods return NSComparisonResult:

- (NSComparisonResult) compare:(NSString *)string;

You could have a line such as this in your program:

if ([userName compare:savedName] == NSOrderedSame)
     // …Do something

You’re actually testing if the method returned 0…but the typedef and enum made that a lot clearer.

Leave a comment


  1. Evede

     /  February 3, 2012


  2. Thank you, this helped me grasp is a lot better. Most sources I’ve used attempted to explain this in one or two lines.

  3. Sweet! This is one of the many things you can pick up informally from reading around, but it makes everyone’s life better when a comprehensive explanation like this is given. Thanks!

  1. Learn Objective-C in 24 Days « Programming for iOS
  2. Enum and typedefs « jamesdevnote
  3. iOS Links – Resources I Collected While Learning iOSThe PHP Coder's Judy Chop | The PHP Coder's Judy Chop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

  • Welcome

    My goal is to make CupsOfCocoa into a beautiful source for beginners to the iPhone platform to get started. Subscribe below for more, and stay tuned!

  • Contact Me

    If you need to contact me for any reason, feel free to send me an email.
  • The Giving Spirit

    If you've found this site helpful, would you consider donating a little sum? Any amount is appreciated...Thanks so much!

  • Roadmap

  • Enter your email address to follow this blog and receive notifications of new posts by email.

    Join 220 other followers

  • Back to the Past

    March 2011
    S M T W T F S
  • Time Machine

  • You count!

  • Worldwide Stats

    free counters
%d bloggers like this: