views:

305

answers:

3

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];
+2  A: 

You can wrap your enum values in a NSNumber object:

[NSNumber numberWithInt:green];
weichsel
+8  A: 

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];

And retrieve it like this:

MyColors theGreenColor = [[list objectAtIndex:0] intValue];

macatomy
A: 

Macatomy's answer is correct. But instead of NSNumber I would suggest you use NSValue. That is its purpose in life.

nicktmro