简体   繁体   中英

Iterate and mutate an Array of Dictionaries Swift 3

I am referencing this in the current Swift documentation:

You can also iterate over a dictionary to access its key-value pairs. Each item in the dictionary is returned as a (key, value) tuple when the dictionary is iterated, and you can decompose the (key, value) tuple's members as explicitly named constants for use within the body of the for-in loop. Here, the dictionary's keys are decomposed into a constant called animalName, and the dictionary's values are decomposed into a constant called legCount.

let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]

for (animalName, legCount) in numberOfLegs {

 print("\\(animalName)s have \\(legCount) legs") } 

// ants have 6 legs // spiders have 8 legs // cats have 4 legs

However, when I create an array of dictionaries dynamically, that code does not function:

let wordArray = ["Man","Child","Woman","Dog","Rat","Goose"]
var arrayOfMutatingDictionaries = [[String:Int]]()

var count = 0

while count < 6
{
    arrayOfMutatingDictionaries.append([wordArray[count]:1])
    count += 1
}

The above routine successfully creates the array of dictionaries, as it should, but when I try to iterate through it like the documentation shows:

for (wordText, wordCounter) in arrayOfMutatingDictionaries

I get an error: Expression type [[String:Int]] is ambiguous without more context

I do not understand that at all.

The goal here is to have a mutable array of mutable dictionaries. Over the course of the program, I want to add new Key-Value pairs, but also be able to increment the values if necessary. I am not married to this collection type, but I thought it would work.

Any ideas?

You are trying to iterate through an array treating it like a dictionary. You'll have to iterate through the array and then through your key/value pairs

for dictionary in arrayOfMutatingDictionaries{
    for (key,value) in dictionary{
        //Do your stuff
    }
}

Adding a key/value pair is pretty straightforward.

for i in 0..< arrayOfMutatingDictionaries.count{
    arrayOfMutatingDictionaries[i][yourkey] = yourvalue
}

You can also increment the existing values like this

for i in 0..<arrayOfMutatingDictionaries.count{
    for (key,value) in arrayOfMutatingDictionaries[i]{
        arrayOfMutatingDictionaries[i][key] = value+1
    }
}
let wordArray = ["Man","Child","Woman","Dog","Rat","Goose"]
var arrayOfMutatingDictionaries = [[String : Int]]()

var count = 0

while count < 6 {
    arrayOfMutatingDictionaries.append([wordArray[count] : 1])
    count += 1
}

for dictionary in arrayOfMutatingDictionaries { // You missed this out!
    for (word, num) in dictionary {
    print(word, num)
    }
}

Try this. And You will get your expected result.

let wordArray = ["Man","Child","Woman","Dog","Rat","Goose"]
var arrayOfMutatingDictionaries = [String]()  
//Here you are doing mistake in above line. You are creating an array of  dictionary ([[String:Int]]) in your code. And you are iterating an array of string (wordArray)

var count = 0

while count < 6
{
     arrayOfMutatingDictionaries.append(wordArray[count])
     count += 1
}

for (wordText) in arrayOfMutatingDictionaries
{
     print(wordText)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM