swift - 字典的 For 循环不遵循它在 Swift 中的顺序

标签 swift for-loop dictionary

<分区>

这是我的代码:

var keys: [String:String] = ["id": "ID", "domain": "Domain", "bundled_domain": "Bundled Domain", "dns": "DNS", "registrar": "Registrar", "registrar_url": "Registrar URL", "sponsoring_registrar": "Sponsoring Registrar", "created": "Created", "updated": "Updated", "expires": "Expires", "whois_server": "Whois Server", "status": "Status"]

for (key, name) in keys{
    println(key)
}

但结果不符合字典的顺序:

the result didn't follow order of the dictionary

那么如何让我的 for 循环遵循我的字典顺序呢?谢谢!

最佳答案

来自 Apples Document :

Swift’s Dictionary type does not have a defined ordering. To iterate over the keys or values of a dictionary in a specific order, use the global sorted function on its keys or values property.

关于swift - 字典的 For 循环不遵循它在 Swift 中的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31722862/

相关文章:

java - 我的 for 循环不会中断,无限打印数组

c++ - for range 循环如何推断普通数组大小

python - 循环添加记录到字典中

ios - 应用程序被终止后保持 Facebook 访问 iOS Swift

ios - 如何在第 2 页(共 3 页)上使用 UIScrollView 禁用水平滚动?

java - 我该如何跳出这个循环? java

python - 用点替换逗号并转换 float 中的值(python 字典)

java - 从具有相似对象 ID 的两个列表中获取 map

ios - Realm 通过其属性查询对象属性字段

ios - 字符串没有在 ios Swift 中正确转换为日期