开发者

hash of array in objective-c, how?

开发者 https://www.devze.com 2022-12-30 18:41 出处:网络
How is a hash of integer array can be represented in objective-c? Here is the ruby hash as an example:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消