Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: get enum valueName from value



2005/08/11 v 19:32, YL:
I defined the following as a consistent convention for
my business objects' status (the mapping here is quite arbitrary, i don't
care:-):


typedef enum _EObjectStatus {
    STATUS_Pending  = -5,
    STATUS_Incomplete  = -4,
    STATUS_Expired  = -3,
    STATUS_Disabled  = -2,
    STATUS_Denied  = -1,
    STATUS_Active  = 1,
    STATUS_Complete  = 2,
    STATUS_Reserved  = 3,
    STATUS_Paid  = 4,
    STATUS_Confirmed  = 5,
} EObjectStatus;

It works quite handy but sometimes i need to display the status valueName
for a runtime status value.
Of course i can define a dictionary to find out value name from value, but
is there a better solution?
(without duplicated mapping definition?)

Names of enums are not preserved after compiling the code (unless you have debugging information enabled) and therefore aren't accessible.


You can use a different solution than enums, for example statically allocated NSStrings, which are quite common in Cocoa:

    NSString * const STATUS_Pending     = @"STATUS_Pending";
    NSString * const STATUS_Incomplete  = @"STATUS_Incomplete";

It's possible to create a custom type and avoid the mapping with a macro (although I would rather avoid macros like this):

    typedef NSString * const EObjectStatus;
    #define DefineEObjectStatus(name) EObjectStatus (name) = @#name;

    DefineEObjectStatus(STATUS_Pending);
    DefineEObjectStatus(STATUS_Incomplete);

No matter which way you choose, you can use the values both as strings and for comparing with the STATUS_ constants.

--
Adam

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/email@hidden

This email sent to email@hidden
References: 
 >Sending floats using objc_msgSendv (From: Nathaniel Gray <email@hidden>)
 >Re: Sending floats using objc_msgSendv (From: Greg Parker <email@hidden>)
 >Re: Sending floats using objc_msgSendv (From: Sherm Pendley <email@hidden>)
 >Re: Sending floats using objc_msgSendv (From: Nathaniel Gray <email@hidden>)
 >Re: Sending floats using objc_msgSendv (From: Sherm Pendley <email@hidden>)
 >get enum valueName from value (From: "YL" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.