Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

ios

swift

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

like image 983
Ajay Avatar asked Dec 26 '22 02:12

Ajay


1 Answers

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:

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
like image 133
vacawama Avatar answered Mar 16 '23 00:03

vacawama