Im digging for ways to enum objc object such as NSString, I remember there a new feature in a version of Xcode4+ which offering a new way to enum , but not clearly. Anyone know that?
You can create Enum from String by using Enum. valueOf() method. valueOf() is a static method that is added on every Enum class during compile-time and it's implicitly available to all Enum along with values(), name(), and cardinal() methods.
There are two ways to convert an Enum to String in Java, first by using the name() method of Enum which is an implicit method and available to all Enum, and second by using toString() method.
The Parse method in Enum converts the string representation of the name or numeric value of enum constants to an equivalent enumerated object.
The valueOf() method of the Enum class in java accepts a String value and returns an enum constant of the specified type.
OK, I answered myself. Guess I make a mistake.
This is the new feature I mentioned above:
typedef enum Language : NSUInteger{ ObjectiveC, Java, Ruby, Python, Erlang }Language;
It's just a new syntax for enum in Xcode 4.4, but I'm so foolish to think we can exchange "NSUInteger" to "NSString".
So here is the way I found that works:
http://longweekendmobile.com/2010/12/01/not-so-nasty-enums-in-objective-c/
// Place this in your .h file, outside the @interface block typedef enum { JPG, PNG, GIF, PVR } kImageType; #define kImageTypeArray @"JPEG", @"PNG", @"GIF", @"PowerVR", nil ... // Place this in the .m file, inside the @implementation block // A method to convert an enum to string -(NSString*) imageTypeEnumToString:(kImageType)enumVal { NSArray *imageTypeArray = [[NSArray alloc] initWithObjects:kImageTypeArray]; return [imageTypeArray objectAtIndex:enumVal]; } // A method to retrieve the int value from the NSArray of NSStrings -(kImageType) imageTypeStringToEnum:(NSString*)strVal { NSArray *imageTypeArray = [[NSArray alloc] initWithObjects:kImageTypeArray]; NSUInteger n = [imageTypeArray indexOfObject:strVal]; if(n < 1) n = JPG; return (kImageType) n; }
FYI. The original author of the second example code created a category for enum handling. Just the thing for adding to your very own NSArray class definition.
@interface NSArray (EnumExtensions) - (NSString*) stringWithEnum: (NSUInteger) enumVal; - (NSUInteger) enumFromString: (NSString*) strVal default: (NSUInteger) def; - (NSUInteger) enumFromString: (NSString*) strVal; @end @implementation NSArray (EnumExtensions) - (NSString*) stringWithEnum: (NSUInteger) enumVal { return [self objectAtIndex:enumVal]; } - (NSUInteger) enumFromString: (NSString*) strVal default: (NSUInteger) def { NSUInteger n = [self indexOfObject:strVal]; if(n == NSNotFound) n = def; return n; } - (NSUInteger) enumFromString: (NSString*) strVal { return [self enumFromString:strVal default:0]; } @end
Alternative way to use struct:
extern const struct AMPlayerStateReadable { __unsafe_unretained NSString *ready; __unsafe_unretained NSString *completed; __unsafe_unretained NSString *playing; __unsafe_unretained NSString *paused; __unsafe_unretained NSString *broken; } AMPlayerState; const struct AMPlayerStateReadable AMPlayerState = { .ready = @"READY", .completed = @"COMPLETE", .playing = @"PLAYING", .paused = @"PAUSED", .broken = @"BROKEN" };
Then you can use like this:
NSString *status = AMPlayerState.ready;
Easy to use, readable. Would be nice if someone update/edit answer with advantages/disadvantages of this approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With