Objective-c: NSString to enum

后端 未结 6 1634
滥情空心
滥情空心 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 05:01

    You could also put the values in an array.

    NSArray *carColorsArray = @[@"red", @"blue", @"white"];
    

    You can then use indexOfObject to get the index of a particular string.

    car_colors carColor = [carColorsArray indexOfObject:@"blue"] + 1;
    

提交回复
热议问题