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开发者_JAVA技巧
hopefully easy to serialize too. Thanks!
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]);
You're looking for a data structure called a map / associative array.
Take a look at this question: HashTables in Cocoa
精彩评论