【问题标题】:Swift: Dictionary of strings with a unique keys and a groups of values that match the keysSwift:具有唯一键和与键匹配的一组值的字符串字典
【发布时间】:2016-05-01 20:18:43
【问题描述】:

我们如何在 Swift 中创建排序字典,就像示例中一样?

数组中的值数据是随机的。值数组可能更长。

var exampleData = [ [["Section1" : ["11","Green"]   ],
                     [["Section1" : ["23","Blue"]    ],
                     [["Section1" : ["344","Yellow"]  ],
                     [["Section2" : ["100","Tall"]    ],
                     [["Section2" : ["232","Short"]   ],
                     [["Section3" : ["112","Number"]  ],
                     [["Section3" : ["222","String"]  ],
var sortedData = [["Section1" : [ ["11","Green"],  ["23","Blue"],["344","Yellow"] ]],
                  ["Section2" : [ ["100","Tall"],  ["232","Short"] ]],
                  ["Section3" : [ ["112","Number"],["222","String"] ]]]

【问题讨论】:

  • 你试过什么?您能否向我们展示您的实现,以便我们更好地理解它?

标签: arrays string swift dictionary


【解决方案1】:

你没有。但是您可以将所有值拉入一个元组数组并对其进行排序。或者只是从一个元组数组开始,并且仅在以下情况下添加值:

  1. 如果键值是唯一的(元组中每个数组索引处的第一个值),或者
  2. 如果键值已存在,则使用要输入的新元组更新值元组。

确保在每次添加新项目后采取措施。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 2021-12-28
    • 1970-01-01
    • 2021-06-24
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    相关资源
    最近更新 更多