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!

2 Answers 2

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]);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Take a look at this question: HashTables in Cocoa

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.