作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的问题是因为 objective-c 中的枚举本质上是一个 int 值,我无法将它存储在 NSMutableArray
中。显然 NSMutableArray
不会采用任何 c 数据类型,例如 int。
有什么通用的方法可以实现吗?
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];
最佳答案
在将枚举值放入数组之前将其包装在 NSNumber 中:
NSNumber *greenColor = [NSNumber numberWithInt:green];
NSNumber *redColor = [NSNumber numberWithInt:red];
NSNumber *blueColor = [NSNumber numberWithInt:blue];
NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:
greenColor,
blueColor,
redColor,
nil];
然后像这样检索它:
MyColors theGreenColor = [[list objectAtIndex:0] intValue];
关于iphone - 如何在 NSMutableArray 中存储枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2487587/
我是一名优秀的程序员,十分优秀!