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

Aja*_*jay 4 xcode ios swift

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

vac*_*ama 9

这是一个显示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)")
    }
}
Run Code Online (Sandbox Code Playgroud)

这输出:

first: Fred
last: Jones
first: Joe
last: Smith
Run Code Online (Sandbox Code Playgroud)

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

last: Jones
first: Fred
last: Smith
first: Joe
Run Code Online (Sandbox Code Playgroud)