Swift:字符串字典,带有唯一键和一组与键匹配的值

时间:2016-05-01 20:18:43

标签: arrays string swift dictionary

我们如何在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"] ]]]

1 个答案:

答案 0 :(得分:1)

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

  1. 如果键值是唯一的(每个数组索引处元组中的第一个值),或
  2. 如果键值预先存在,则使用要输入的新元组更新值元组。
  3. 请务必在每次添加新项目后求助。