views:

30

answers:

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!

+3  A: 
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
+1  A: 

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

Take a look at this question: http://stackoverflow.com/questions/474316/hashtables-in-cocoa

sirhc