In 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_AAPLListInfo_m-DontLinkElementID_35
they have this code to define additional undefined state of color enum
#define AAPLListColorUndefined ((AAPLListColor)-1)
...
#undef AAPLListColorUndefined
but why don't they just add this to the class where they define others color
typedef NS_ENUM(NSInteger, AAPLListColor) {
AAPLListColorGray = 0,
AAPLListColorBlue,
AAPLListColorGreen,
AAPLListColorYellow,
AAPLListColorOrange,
AAPLListColorRed
};
Is there a reason behind this ? or just a personal style without any benefit.
Note that AAPLListColorUndefined
is used only in the file it's declared (and is even specifically undefined at the end of that file.) It's basically a helper value that's only used in that class (to keep track of whether the colour has been set yet.) Unlike the other values, it's only useful in that one class. So, that's design reason one: it's not really a colour value; it's basically just a flag used in that specific class.
A second reason is that the compiler will check NS_ENUM values for completeness in switch
statements. Try adding AAPLListColorUndefined into the enum itself—you'll now see compiler warnings for switch
statements 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 the undefined value isn't being handled. As the value can only be undefined in that one specific class, it makes sense not to have it available in the NS_ENUM for the other classes, as that would force you to handle it, when you know your colour value can't logically can't ever be undefined in them. (In the other files, a default grey colour is used, so there's no need for an "undefined" value—the colour is always defined.)
So, that's kind of the "flip side" of the design reason: as well as only being useful in the one file it's defined, it's specifically not useful in the files it's not being used in.
This is very tidy code, I'd say. If you were to add the undefined colour as part of the enum, then you'd need to do something in every switch statement that used the enum, even if it was only having a default:
case that did nothing, or raised an error that you knew would never happen.
Or, you could get away without having the undefined value at all in AAPLListInfo.m
by setting the colour property to a default value, say grey, like the other files, but then you'd need an extra flag—a boolean, probably—to indicate whether the list info has been fetched.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments