Created
July 12, 2012 14:06
-
-
Save hezi/3098340 to your computer and use it in GitHub Desktop.
Objective-C Enum-TO-NSString and Vice versa.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Declare enums like so: | |
#define IMAGE_STATUS(XX) \ | |
XX(kDOImageStatusOK, = 0) \ | |
XX(kDOImageStatusCached, )\ | |
XX(kDOImageStatusRetry, ) | |
DECLARE_ENUM(DOImageStatus, IMAGE_STATUS) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define Enum-To-NSString functions like so: | |
DEFINE_ENUM(DOImageStatus, IMAGE_STATUS) | |
// use them like this: | |
NSString *imageStatus = NSStringFromDOImageStatus(kDOImageStatusOK); | |
DOImageStatus statusFromString = DOImageStatusFromNSString(@"kDOImageStatusCached"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Based on http://stackoverflow.com/a/202511 | |
// | |
#pragma mark - Enum Factory Macros | |
// expansion macro for enum value definition | |
#define ENUM_VALUE(name,assign) name assign, | |
// expansion macro for enum to string conversion | |
#define ENUM_CASE(name,assign) case name: return @#name; | |
// expansion macro for string to enum conversion | |
#define ENUM_STRCMP(name,assign) if (![string isEqualToString:@#name]) return name; | |
/// declare the access function and define enum values | |
#define DECLARE_ENUM(EnumType,ENUM_DEF) \ | |
typedef enum EnumType { \ | |
ENUM_DEF(ENUM_VALUE) \ | |
}EnumType; \ | |
NSString *NSStringFrom##EnumType(EnumType value); \ | |
EnumType EnumType##FromNSString(NSString *string); \ | |
// Define Functions | |
#define DEFINE_ENUM(EnumType, ENUM_DEF) \ | |
NSString *NSStringFrom##EnumType(EnumType value) \ | |
{ \ | |
switch(value) \ | |
{ \ | |
ENUM_DEF(ENUM_CASE) \ | |
default: return @""; \ | |
} \ | |
} \ | |
EnumType EnumType##FromNSString(NSString *string) \ | |
{ \ | |
ENUM_DEF(ENUM_STRCMP) \ | |
return (EnumType)0; \ | |
} |
@fredwork you can format your code by putting triple backticks before and after it: https://help.github.com/articles/github-flavored-markdown/#fenced-code-blocks
(The Objective-C language specifier is objc
)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hmm.. I was disappointed how github left-justified all my lines of code.
I suggest they improve this as it's common for people to supply code in the replies...