我的问题是因为在Objective-c中的枚举本质上是一个int值,我不能将它存储在NSMutableArray
中。显然NSMutableArray
不会像int那样采用任何c数据类型。如何将枚举值存储在NSMutableArray中
是否有任何常见的方法来实现这一目标?
typedef enum
{
green,
blue,
red
} MyColors;
NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:
green,
blue,
red,
nil];
//Get enum value back out
MyColors greenColor = [list objectAtIndex:0];
一般来说,这应该是安全的,但有些时候它不是,因为枚举可表示内部为各种不同的类型。看到这个答案替代http://stackoverflow.com/questions/1187112/cocoa-dictionary-with-enum-keys/1187901#1187901 – DougW 2011-07-20 18:11:36
好的解决方案,谢谢 – 2017-08-10 13:20:02