Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to enum NSString

Tags:

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?

like image 882
kimimaro Avatar asked Nov 01 '12 06:11

kimimaro


People also ask

How do you map a string to an Enum?

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.

How do I create an Enum string?

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.

Can you parse an Enum?

The Parse method in Enum converts the string representation of the name or numeric value of enum constants to an equivalent enumerated object.

Can we convert string to Enum in Java?

The valueOf() method of the Enum class in java accepts a String value and returns an enum constant of the specified type.


2 Answers

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 
like image 85
kimimaro Avatar answered Sep 21 '22 15:09

kimimaro


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.

like image 35
Injectios Avatar answered Sep 23 '22 15:09

Injectios