How to navigate an array <Dictionary <String, String >> using Swift

Give me one example and explanation for "loop through array<Dictionary<String,String>>

with swift ...

+3


source share


1 answer


Here is an example showing 2 loops. The first loops through the array collect each dictionary. The second loop goes through the dictionary, selecting each key, a pair of values:

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

      

Output:



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

      

Note that dictionaries are unordered, so this can print as well:

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

      

+8


source







All Articles