Hash of an array in objective-c, how?

How can a hash of an integer array be represented in objective-c? Here's an example of a ruby ​​hash:

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

      

which can be accessed:

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

      

hopefully easy to serialize. Thanks!

+2


a source to share


2 answers


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]);

      



+3


a source


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



Take a look at this question: Hash tables in Cocoa

+1


a source







All Articles