xcode 如何使用 Swift 遍历 Array<Dictionary<String,String>>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26727789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:05:10  来源:igfitidea点击:

How to loop through Array<Dictionary<String,String>> using Swift

iosxcodeswift

提问by Ajay

Give me one Example and explanation for "loop through array<Dictionary<String,String>>using swift...

给我一个例子和解释“array<Dictionary<String,String>>使用 swift循环......

回答by vacawama

Here is an example that shows 2 loops. The first loops through the array picking out each dictionary. The second loop loops through the dictionary picking out each key, value pair:

这是一个显示 2 个循环的示例。第一个循环遍历数组,挑选出每个字典。第二个循环遍历字典,挑选出每个键值对:

let people:Array<Dictionary<String,String>> = [["first":"Fred", "last":"Jones"], ["first":"Joe", "last":"Smith"]]

// Grab each person dictionary from the array of dictionaries
for person in people {
    // Grab each key, value pair from the person dictionary
    // and print it
    for (key,value) in person {
        println("\(key): \(value)")
    }
}

This outputs:

这输出:

first: Fred
last: Jones
first: Joe
last: Smith

Note that dictionaries are unordered, so this could also print:

请注意,字典是无序的,因此这也可以打印:

last: Jones
first: Fred
last: Smith
first: Joe