字典(键,值)顺序 [英] Dictionary (key , value) order
问题描述
假设我有以下代码
var dictionary = ["cat": 2,"dog":4,"snake":8]; // mutable dictionary
var keys = dictionary.keys
var values = dictionary.values
for e in keys {
println(e)
}
for v in values {
println(v)
}
将字典例如,如果dictionary.keys是dog,snake,cat
,那么.keys和dictionary.value具有相同的顺序
would the dictionary.keys and dictionary.values have the same order
dictionary.values总是4,8,2?
i在操场上尝试,结果总是表示他们有相同的顺序
for example if the dictionary.keys is "dog","snake","cat" would the dictionary.values be always 4,8,2? i tried it on the playground and the result always indicated that they have the same order
推荐答案
不,不是保证以同样的顺序。从文档:
No, it is not guaranteed to be in the same order. From documentation:
Swift's Dictionary类型是一个无序的集合。未指定
键,值和键值对的顺序在
字典上迭代时的顺序。
Swift’s Dictionary type is an unordered collection. The order in which keys, values, and key-value pairs are retrieved when iterating over a dictionary is not specified.
这篇关于字典(键,值)顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!