I need to create a dictionary/hashmap where the
NSObject
NSDictionary
won't work here (enums don't conform to NSCopying
).
I could perhaps use a CFDictionaryRef
here, but I'd like to know if is there any other way to achieve this.
Since enums are integers, you can wrap the enum in an NSNumber. When you add/retreive something to/from the map, you pass the enum to the NSNumber constructor...
Assuming you've got an enum like...
enum ETest { FOO, BAR };
You can use it in an NSDictionary like this...
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject: @"Foo!" forKey:[NSNumber numberWithInt: FOO]]; NSLog(@"getting value for FOO -> %@", [dict objectForKey: [NSNumber numberWithInt: FOO]]); [dict release];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With