2

How is a hash of integer array can be represented in objective-c? Here is the ruby hash as an example:

hi_scores = { "John" => [1, 1000],
              "Mary" => [2, 8000],
              "Bob"  => [5, 2000] }

such that can be accessed by:

puts hi_scores["Mary"][1]
=> 8000

hopefully easy to serialize too. Thanks!

ohho
  • 50,879
  • 75
  • 256
  • 383

2 Answers2

3
NSDictionary * highScores = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:1000], nil], @"John",
                                                                       [NSArray arrayWithObjects:[NSNumber numberWithInt:2], [NSNumber numberWithInt:8000], nil], @"Mary",
                                                                       [NSArray arrayWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:2000], nil], @"Bob", nil];

NSLog(@"%@", [[highScores objectForKey:@"Mary"] objectAtIndex:1]);
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
1

You're looking for a data structure called a map / associative array.

Take a look at this question: HashTables in Cocoa

Community
  • 1
  • 1
sirhc
  • 6,097
  • 2
  • 26
  • 29