Objective-c: NSString to enum

后端 未结 6 1632
滥情空心
滥情空心 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:55

    I found the solution:

    if ([car_color isEqualToString:@"1"])
            return red;
    if ([tipo_pdi isEqualToString:@"2"])
            return blue;
    if ([tipo_pdi isEqualToString:@"3"])
            return white;
    

    But I don't like this 'if' style, what if I had a thousand colors? Isn't there a more automatic solution?

提交回复
热议问题