0
votes

In Ruby, we have symbols to use for the key of hashes. I'm trying to port a Ruby library to Objective-C, and the library has a hash in it that uses symbols as keys. Is there any similar soulution for Objective-C? Or should I be using NSStrings?

2
Objective-C is a super-set of C, so you might be able to use #define constants to achieve a similar outcome.Sahand
call a to_s on all the symbols, and you have strings available to use as your keys (symbols are mutable strings)bjhaid
I;m talking about symbols in Objective-C, not Ruby.Linuxios

2 Answers

2
votes

I've seen declarations like

extern NSString *const NSKeyValueChangeNewKey = @"NSKeyValueChangeNewKey";

so that you can use it as a key:

[dict objectForKey:NSKeyValueChangeNewKey];

For an explanation that's a bit more detailed, see Constants in Objective-C.

0
votes

NSDictionary keys in Objective-C are usually NSStrings. That's probably the way to go here.

You don't have to worry about Ruby string literals vs. symbols; just create an NSString with the string value for the key or use a literal @"my key name" string as required.