Swift-由什么决定词典集合的顺序? [英] Swift - what determines the order of a Dictionary Collection?

查看:54
本文介绍了Swift-由什么决定词典集合的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我说顺序时-我的意思是编译器选择显示结果的顺序,我知道字典没有像数组那样的索引.

When I say order - I mean the order that the compiler chooses to display the results, I know that a dictionary has no index like an array does.

我有以下字典:

let groups :Dictionary<String,AnyObject> = [
"Data": ["Save", "Restore"],
"Load Tabs": ["Reload Tabs when selecting tab"],
"Privacy": ["Set Passcode"],
"About Me": ["Twitter", "LinkedIn"]]

但是控制台显示如下:

[隐私" :( 设置密码" ),加载标签" :( 选择选项卡时重新加载选项卡" ), 数据": ( 救, 恢复 ), 关于我": ( 推特, 领英 )]

["Privacy": ( "Set Passcode" ), "Load Tabs": ( "Reload Tabs when selecting tab" ), "Data": ( Save, Restore ), "About Me": ( Twitter, LinkedIn )]

您可以看到顺序不同,但是当我更改字典代码的顺序时,输出仍然相同.

As you can see the order is different, but when I change the order of the dictionary code the output is still the same.

所以有人可以为我澄清一下,以便我更好地理解这是如何实现的吗?我不是要操纵字典,而是要了解如何确定输出.

So could someone clarify that for me please so I understand better how this is achieved? I am not trying to manipulate the dictionary, but trying to understand how the output is determined.

如果键没有设置顺序,那么每次字典给出输出时,键是否应该随机出现?

If keys don't have a set order, should it not appear random each time the dictionary gives an output?

推荐答案

DictionaryHashable键进行操作,并使用

Dictionarys operate on Hashable keys and are implemented using a hash table, a frequently used way to represent associative arrays with O(1) lookup. Hash Tables are usually sorted by the integer value of the hash code for small numbers of key-value-pairs (below the bucket threshold), and after that in reverse insertion order. This means you cannot rely on hash tables to be sorted in any reasonable or predictable order in most cases.

这篇关于Swift-由什么决定词典集合的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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