What is the reason for defining an unknown enumeration in a separate class?

Apple Lister example https://developer.apple.com/library/ios/samplecode/Lister/Listings/Objective_C_ListerKit_AAPLListInfo_m.html#//apple_ref/doc/uid/TP40014701-Objective_C_ListerKit_AAPLLIDLinkInfo_m-Dont35

they have this code to define extra undefined state of enum color

#define AAPLListColorUndefined ((AAPLListColor)-1)
...
#undef AAPLListColorUndefined

      

but why don't they just add this to the class where they define other colors

typedef NS_ENUM(NSInteger, AAPLListColor) {
    AAPLListColorGray = 0,
    AAPLListColorBlue,
    AAPLListColorGreen,
    AAPLListColorYellow,
    AAPLListColorOrange,
    AAPLListColorRed
};

      

Is there any reason for this? or just personal style without any benefit.

+3


source to share


1 answer


Note that this AAPLListColorUndefined

is only used in the declared file (and even specifically undefined at the end of this file.) This is basically a helper value that is only used in this class (to keep track of the color is still set.) Unlike other values, this is only useful for one class. So this design reason is one: it's not really the meaning of color; it is basically just a flag used in this particular class.

The second reason is that the compiler will check NS_ENUM values ​​for completeness in statements switch

. Try adding AAPLListColorUndefined to enum yourself - now you will see compiler warnings for statements switch

like this:

// Warning: Enumeration value 'AAPLListColorUndefined' not handled in switch
switch (listColor) {
    case AAPLListColorGray:     return _grayColor;
    case AAPLListColorBlue:     return _blueColor;
    case AAPLListColorGreen:    return _greenColor;
    case AAPLListColorYellow:   return _yellowColor;
    case AAPLListColorOrange:   return _orangeColor;
    case AAPLListColorRed:      return _redColor;
}

      

... because the compiler can see that undefined is not being processed. Since the value can only be undefined in this particular class, it makes sense not to have it in NS_ENUM for other classes, as that will force you to deal with it when you know your color value cannot logically be undefined in them. (Other files use gray by default, so there is no need for "undefined" - the color is always defined.)



So, this kind of "flip-side" for design reasons, and also only useful in one file that it defines, it is specifically not useful in files that don't use it.

This is very neat code, I would say. If you were to add undefined color as part of an enum, then you would need to do something in every switch statement that used the enum, even if it only had an event default:

that did nothing, or raised an error you knew you never did will happen.

Or you can get away with no undefined value at all in AAPLListInfo.m

by setting the color property to a default value such as gray like other files, but then you need an additional boolean flag, probably to indicate if the list information has been selected.

+3


source







All Articles