【问题标题】:hash of array in objective-c, how?Objective-C中数组的哈希值,如何?
【发布时间】:2010-05-20 04:40:05
【问题描述】:

整数数组的散列如何在objective-c中表示?这里以 ruby​​ 哈希为例:

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

这样可以通过以下方式访问:

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

希望也易于序列化。谢谢!

【问题讨论】:

    标签: objective-c data-structures


    【解决方案1】:
    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]);
    

    【讨论】:

      【解决方案2】:

      您正在寻找一个名为 map / associative array 的数据结构。

      看看这个问题: HashTables in Cocoa

      【讨论】:

        猜你喜欢
        • 2016-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-15
        • 1970-01-01
        • 2011-02-05
        • 1970-01-01
        相关资源
        最近更新 更多