【问题标题】:What's the shortest bit of code that returns keys/elements from a dictionary in random order?从字典中以随机顺序返回键/元素的最短代码是什么?
【发布时间】:2012-05-18 14:44:02
【问题描述】:

我希望以随机、非重复的顺序访问字典中的键/元素。最短的 ios 代码是什么?

【问题讨论】:

    标签: ios sorting


    【解决方案1】:

    一旦你拥有来自canonical way to randomize an NSArray in Objective C 的 NSMutableArray 的 -shuffle 类别。

    NSMutableArray *suffledKeys = [[dict allKeys] mutableCopy];
    [suffledKeys shuffle];
    for (id randomKey in shuffledKeys) {
        id randomValue = [dict objectForKey:randomKey];
        // What ever you need to do.
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-13
      • 2016-06-19
      • 2018-08-02
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多