Objective-c: NSString to enum

后端 未结 6 1641
滥情空心
滥情空心 2021-01-31 04:24

So, I\'ve got this definition:

typedef enum {
    red = 1,
    blue = 2,
    white = 3
} car_colors;

Then, I\'ve got a variable of type car_col

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 04:37

    // ...
    typedef enum {
        One = 0,
        Two,
        Three
    } GFN;
    // ...
    #define kGFNPrefix @"GFNEnum_"
    // ...
    + (NSString *)gfnToStr:(GFN)gfn {
        return [NSString stringWithFormat:@"%@%d", kGFNPrefix, gfn];
    }
    
    + (GFN)gfnFromStr:(NSString *)str {
        NSString *gfnStr = [str stringByReplacingOccurrencesOfString:kGFNPrefix withString:@""];
        return [gfnStr intValue];
    }
    // ...
    

    My choice =)

提交回复
热议问题