本文介绍了如何在NSMutableArray中存储枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是因为objective-c中的枚举本质上是一个int值,我不能将它存储在 NSMutableArray
。显然 NSMutableArray
不会像任何c类型的数据类型。
My problem is since an enum in objective-c essentially is an int value, I am not able to store it in a NSMutableArray
. Apparently NSMutableArray
won't take any c-data types like an int.
有没有什么常见的方法来实现这一点?
Is there any common way to achieve this ?
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之前数组:
Wrap the enum value in an NSNumber before putting it in the array:
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];
这篇关于如何在NSMutableArray中存储枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!