Saving enum values into a dictionary

user773578 picture user773578 · Jul 12, 2011 · Viewed 17.1k times · Source

How does one save an enum value to a dictionary?

When I try the following

enum someEnum
{
    field0 = 0,
    field1 = 1,
    field2 = 2,
};

enum someEnum someEnumObject;

and I try to save it to a dictionary using

[NSDictionary dictionaryWithObjectsAndKeys:]

someEnumObject, @"enum", 

I get this

warning: Semantic Issue: Incompatible integer to pointer conversion sending 'enum behaviour' to parameter of type 'id'

Answer

EmptyStack picture EmptyStack · Jul 12, 2011

Use the following to save it to dictionary,

[NSNumber numberWithInt:enumValue], @"enum",

And you can retrieve it as,

enumValue = [[dictionary valueForKey:@"enum"] intValue];