ios - 相同顺序索引 Swift 1.2 中的字典值和键

标签 ios arrays swift dictionary

我试试这个

let dict: [String:String] = ["1":"one","2":"two","3":"three","4":"four"]
let keyArray = [String](dict.keys)
let valueArray = [String](dict.values)

println(dict)
println(keyArray)
println(valueArray)

我希望结果应该是

//dict = ["1":"one","2":"two","3":"three","4":"four"]
//keyArray = ["1","2","3","4"]
//valueArray = ["one","two","three","four"]

但事实并非如此

//dict = ["4": "four", "2": "two", "1": "one", "3":"three"]
//keyArray = ["4","2","1","3"]
//valueArray = ["four", "two", "one", "three"]

如何得到我希望的结果

感谢您的帮助

最佳答案

来自 Swift 语言指南

字典存储集合中相同类型的键和相同类型的值之间的关联 没有定义顺序

如果你想要一种排序,获取键,对它们进行排序并获取关联的值。

GitHub 上有自定义类 OrderedDictionary,带有用于保持顺序的键的后备数组。

关于ios - 相同顺序索引 Swift 1.2 中的字典值和键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31388240/

相关文章:

ios - Swift CollectionView第一个单元格添加用户按钮,并从图像数组加载其他单元格

ios - 在 iPhone 7 的顶部和底部扬声器之间平移声音

c - 在表中实现字符串文字及其大小

javascript - Razor MVC 使用模型数组填充 Javascript 数组

ios - swift 3 : How to animate a view background color similar to a progress bar

swift - 我应该把 UI 代码放在 Swift 的什么地方?

Swift 相当于 each_slice

ios - 添加SKNode作为触发器,这样就不会影响其他节点的路径

html - UIWebView 框架高度基于内容高度

c - malloc() 用于字符串数组,但没有像希望的那样工作