【问题标题】:For loop for Dictionary don't follow it's order in Swift [duplicate]字典的 for 循环不要遵循它在 Swift 中的顺序 [重复]
【发布时间】:2015-10-21 18:10:04
【问题描述】:

这是我的代码:

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)
}

但结果并没有按照字典的顺序:

那么我怎样才能让我的 for 循环遵循我的字典顺序呢?谢谢!

【问题讨论】:

标签: swift for-loop dictionary


【解决方案1】:

来自Apples Document

Swift 的 Dictionary 类型没有定义的顺序。迭代 以特定顺序在字典的键或值上,使用 对其键或值属性的全局排序函数。

【讨论】:

    猜你喜欢
    • 2013-01-04
    • 2011-05-06
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2022-11-17
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多