0

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?

Linuxios
  • 34,849
  • 13
  • 91
  • 116

2 Answers2

2

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.

Community
  • 1
  • 1
Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
0

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.

gregheo
  • 4,182
  • 2
  • 23
  • 22
  • @benalpert 's answer is the better answer. The use of a symbol is idiomatic in Ruby because they are immutable and are only created once. In Objective-C a const pointer to a string is the equivalent that you want to use. – Paul.s Apr 02 '12 at 18:26