views:

307

answers:

2

Kind of a weird newbie question...I want to use typedef enum declarations in one of my classes . This particular class gets used by other classes, where a client class might say something like "set your style to Style1 (enumerated type)". Then the target class's behavior would change accordingly. This is analogous to the way that UITableViewCellStyles are used in the iPhone SDK.

So, I read through a few UIKit framework headers to get a better idea of how Apple was handling the enumerated types. I see that they declare a bunch of enums everywhere, like so...

typedef enum {
    UIBarButtonSystemItemDone,
    UIBarButtonSystemItemCancel,
    UIBarButtonSystemItemEdit,  
    UIBarButtonSystemItemSave,  
    UIBarButtonSystemItemAdd,
    ...
    UIBarButtonSystemItemUndo,   // available in iPhone 3.0
    UIBarButtonSystemItemRedo,   // available in iPhone 3.0
} UIBarButtonSystemItem;

...but I don't see any clues in the header on how they actually handle these types (I'm basically trying to see an example of their implementation, so this isn't a suprise). My instinctive thought as a fairly newb programmer would be to match the int values of each type to some behavior/variable stored in an array, plist etc. But also as a newb programmer I expect that everything that I think will be wrong. So I have two questions:

  1. Anybody have a guess as to how Apple itself handles enum type values to change behaviors?
  2. In general for this type of setup, is there a best design practice that everyone knows about or is this just an open-ended scenario?
+2  A: 

An enumeration is (almost but not really) an integer, but made easy to read for the programmer that's using it. Given that, you can have routines that check the value of an enum the same way you would an integer:

void DoMyEnumeratedThing(UIBarButtonSystemItem item)
{
    if (item == UIBarButtonSystemItemDone)
        DoMyItemDoneThing();
    else if (item == UIBarButtonSystemItemCancel)
        DoMyItemCancelThing();
    // ...and so forth and so on.
}

While I don't know the gory details of Apple's OS internals, every enumeration check essentially boils down to something like the above. As for your "best design practice" question, the answer really depends on what you're trying to accomplish with the enumeration. While every use-case is switch-like, sometimes the enumeration is a set of bits in a bitfield that allow the client to toggle one or more of them at the same time (which is not the case in the example you provide).

fbrereto
+6  A: 

Simple enums are usually handled in a switch statement:

typedef enum {
    eRedStyle,
    eGreenStyle,
    eBlueStyle
} MyStyle;

@implementation MyClass

- (void)setStyle:(MyStyle)style {
    switch (style) {
        case eRedStyle :
            self.backgroundColor = [UIColor redColor];
            break;
        case eGreenStyle :
            self.backgroundColor = [UIColor greenColor];
            break;
        case eBlueStyle :
            self.backgroundColor = [UIColor blueColor];
            break;
        default :
            NSLog(@"Bad Style: %d", style);
            break;
    }
}
Darren