Saving enum values into a dictionary Saving enum values into a dictionary ios ios

Saving enum values into a dictionary


Use the following to save it to dictionary,

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

And you can retrieve it as,

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


Better use NSNumber literals to convert an enum to an object so that it can be stored in NSDictionary:

[NSDictionary dictionaryWithObjectsAndKeys:@(someEnumObject), @"enum", nil];

Literals provide shorthands to write stuff, this dictionary can be written like:

@{@"enum":@(someEnumObject)};

Read more about literals here: http://clang.llvm.org/docs/ObjectiveCLiterals.html


An enum is essentially an integer and an NSDictionary stores objects, so you need to convert your enum to an object. An NSNumber would work well for this:

[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:someEnumObject], @"enum", nil];