enum Values to NSString (iOS)

前端 未结 16 1918
悲&欢浪女
悲&欢浪女 2020-12-04 07:57

I have an enum holding several values:

enum {value1, value2, value3} myValue;

In a certain point in my app, I wish to check which value of

16条回答
  •  庸人自扰
    2020-12-04 08:13

    Here is a plug-and-play solution that you can extend with a simple copy and paste of your EXISTING definitions.

    I hope you all find it useful, as I have found useful so many other StackOverflow solutions.

    - (NSString*) enumItemNameForPrefix:(NSString*)enumPrefix item:(int)enumItem {
    NSString* enumList = nil;
    if ([enumPrefix isEqualToString:@"[Add Your Enum Name Here"]) {
        // Instructions:
        // 1) leave all code as is (it's good reference and won't conflict)
        // 2) add your own enums below as follows:
        //    2.1) duplicate the LAST else block below and add as many enums as you like
        //    2.2) Copy then Paste your list, including carraige returns
        //    2.3) add a back slash at the end of each line to concatenate the broken string
        // 3) your are done.
    }
    else if ([enumPrefix isEqualToString:@"ExampleNonExplicitType"]) {
        enumList = @" \
        ExampleNonExplicitTypeNEItemName1, \
        ExampleNonExplicitTypeNEItemName2, \
        ExampleNonExplicitTypeNEItemName3 \
        ";
    }
    else if ([enumPrefix isEqualToString:@"ExampleExplicitAssignsType"]) {
        enumList = @" \
        ExampleExplicitAssignsTypeEAItemName1 = 1, \
        ExampleExplicitAssignsTypeEAItemName2 = 2, \
        ExampleExplicitAssignsTypeEAItemName3 = 4 \
        ";
    }
    else if ([enumPrefix isEqualToString:@"[Duplicate and Add Your Enum Name Here #1"]) {
        // Instructions:
        // 1) duplicate this else block and add as many enums as you like
        // 2) Paste your list, including carraige returns
        // 3) add a back slash at the end of each line to continue/concatenate the broken string
        enumList = @" \
        [Replace only this line: Paste your Enum Definition List Here] \
        ";
    }
    
    // parse it
    int implicitIndex = 0;
    NSString* itemKey = nil;
    NSString* itemValue = nil;
    NSArray* enumArray = [enumList componentsSeparatedByString:@","];
    NSMutableDictionary* enumDict = [[[NSMutableDictionary alloc] initWithCapacity:enumArray.count] autorelease];
    
    for (NSString* itemPair in enumArray) {
        NSArray* itemPairArray = [itemPair componentsSeparatedByString:@"="];
        itemValue = [[itemPairArray objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        itemKey = [NSString stringWithFormat:@"%d", implicitIndex];
        if (itemPairArray.count > 1)
            itemKey = [[itemPairArray lastObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [enumDict setValue:itemValue forKey:itemKey];
        implicitIndex++;
    }
    
    // return value with or without prefix
    NSString* withPrefix = [enumDict valueForKey:[NSString stringWithFormat:@"%d", enumItem]];
    NSString* withoutPrefix = [withPrefix stringByReplacingOccurrencesOfString:enumPrefix withString:@""];
    NSString* outValue = (0 ? withPrefix : withoutPrefix);
    if (0) NSLog(@"enum:%@ item:%d retVal:%@ dict:%@", enumPrefix, enumItem, outValue, enumDict);
    return outValue;
    }
    

    Here are the example declarations:

    typedef enum _type1 {
    ExampleNonExplicitTypeNEItemName1, 
    ExampleNonExplicitTypeNEItemName2, 
    ExampleNonExplicitTypeNEItemName3
    } ExampleNonExplicitType;
    
    typedef enum _type2 {
    ExampleExplicitAssignsTypeEAItemName1 = 1, 
    ExampleExplicitAssignsTypeEAItemName2 = 2, 
    ExampleExplicitAssignsTypeEAItemName3 = 4
    } ExampleExplicitAssignsType;
    

    Here is an example call:

    NSLog(@"EXAMPLE:  type1:%@  type2:%@ ", [self enumItemNameForPrefix:@"ExampleNonExplicitType" item:ExampleNonExplicitTypeNEItemName2], [self enumItemNameForPrefix:@"ExampleExplicitAssignsType" item:ExampleExplicitAssignsTypeEAItemName3]);
    

    Enjoy! ;-)

提交回复
热议问题