字典(键,值)顺序 [英] Dictionary (key , value) order

查看:150
本文介绍了字典(键,值)顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下代码

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆