Enum


#1

push


#2

enum makes it possible to define an integer-valued type for a set of named, constant values.

For example:

enum SwitchState
{
   S_ON,
   S_OFF,
   S_STUCK
};

By default, the names S_ON, S_OFF, and S_STUCK are assigned the sequential values, 0, 1, and 2, respectively.

However, if there is a need, the default assignment of values can be overridden.

Start at 64:

enum SwitchState
{
   S_ON = 64,
   S_OFF, 
   S_STUCK
};

Assign prime numbers:

enum SwitchState
{
   S_ON     = 5,
   S_OFF    = 3,
   S_STUCK  = 2
};

Can be used like this:

enum SwitchState
{
   S_ON,
   S_OFF,
   S_STUCK
};
...
void Print (const enum SwitchState);
...
enum SwitchState lightSwitchState = S_STUCK;
Print (lightSwitchState);
...
void Print (const enum SwitchState s)
{
     printf ("switch state: ");
     switch (s)
     {
       default:
               printf ("kaput\n");
               break;
       case S_ON:
               printf ("on\n");
               break;
       case S_OFF:
               printf ("off"\n");
               break;
      case S_STUCK:
               printf ("stuck\n");
               break:
     }
}

Finally, it can be used with typedef:

typedef enum SwitchState SwitchState;

SwitchState StateOfSwitch (int);
...
SwitchState switch17State = StateOfSwitch (17);
...

#3

I am having trouble figuring out where exactly the code on page 193 to declare the constant NSLocaleCurrencyCode would be in the header and implementation files for NSLocale.

Where exactly would we find this code in NSLocale.h :

extern NSString * const NSLocaleCurrencyCode

And where would we find this code in NSLocale.m:

NSString * const NSLocaleCurrencyCode = @“currency”;

My guess is that for .h, the code would go in curly braces like an explicit instance variable.
My guess is that for .m, the code goes in between the @implementation and @end, but not in any specific function (it can’t go in the class extension because it has to be publicly available to other classes and main() if I understand correctly)

Are my guesses correct? Also, how exactly does this relate to the preprocessor directives.

Thanks!