以快速有序的形式迭代字典中的对象 [英] Iterate object in dictionary with ordered form in swift

查看:73
本文介绍了以快速有序的形式迭代字典中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典,我想以有序的形式迭代它,因为对象在字典中,但是在迭代之后它以随机顺序迭代.请帮忙提前致谢.

I have a dictionary i want to iterate it in ordered form as the objects are in the dictionary, but after iterating it is iterating in random order. Please help Thanks in advance.

推荐答案

您不能对字典进行排序,但可以按如下方式对其键进行排序:

You can't sort a dictionary but you can sort its keys as follow:

let myDict: [String:String] = ["car":"blue", "watch":"black", "bike": "red"]

for key in myDict.keys.array.sorted(<) {
    let value  = myDict[key]!
    println("\(key) = \(value)")
}


let dashboardItemsTuples : [(String , String)] = [("All Cards" , "cards" ), ("Cards by Topic" , "cards_topic"), ("Lessons" , "lesson" ), ("Audio" , "audio"), ("Video" ,"video") ,( "Ebook" , "ebook") , ("Bookmarked Cards" , "bookmark_cards" ), ("Upgrade","upgrade") , ("Downloads" ,"downloads")]

for (key,value) in dashboardItemsTuples {
    println("\(key)=\(value)")
}

这篇关于以快速有序的形式迭代字典中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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