For循环词典不要按照它在Swift中的顺序 [英] For loop for Dictionary don't follow it's order in Swift

查看:119
本文介绍了For循环词典不要按照它在Swift中的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var keys:[String:String] = [id:ID,域名域名bundled_domain捆绑域名dnsDNS注册商注册商注册商注册商URLsponsoring_registrar赞助注册商 created:Created,updated:Updated,expires:Expires,whois_server:Whois服务器,status:状态] 

(key,name)在键{
println(key)
}

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



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

苹果文档


Swift's Dictionary type does not have定义的顺序。要按特定顺序在字典的键或值上迭代
,请在其键或值属性上使用
全局排序函数。


here's my code:

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

But the result didn't follow order of the dictionary:

So how can I make my for loop follow my dictionary order? Thanks!

解决方案

From 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.

这篇关于For循环词典不要按照它在Swift中的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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