Printing nested dictionary values after update only showing updated value

(Ben) #1

Iterating through a nested dictionary, I can only print the full results I am looking for once. Because after updating a result it will only print out that one result, why?

                for (checkKey, iterValue) in pillsTally {
                    if (checkKey == havefeverSeason) {

                        for (checkKeyNested, iterValueNested) in iterValue {
                            if (checkKeyNested == {
                                let increasedValue = (iterValueNested + 1)
                                pillsTally.updateValue([checkKeyNested : increasedValue], forKey: checkKey)
				                iterValue.forEach { print("\($0): \($1)") }

I have tried calling a function with the print statement in, but it will insist on only printing the single updated value and not the full nested array.

Afterwards I would like to print the result in alphabetical order, which I think is sorted with the > operator at the print statement?

Thank you in advance.


Are you trying to do something like this?

pillsTally[havefeverSeason]?[]? += 1


One important aspect of Dictionary is that you can have at most one value associated with any particular key.

You innermost statement is executed only when checkKey == haveFeverSeason and checkKeyNested ==

Because outer loop is iterated over Dictionary, the outer if-statement will succeed no more than once and at that point iterValue will have value of pillTally[haveFeverSeason].
Once inside, the inner for-loop again iterate over Dictionary, so the inner if-statement again succeed no more than once.

Note that no more than once also mean it can succeed 0 times, and you might want you logic to accomodate that.

Since Dictionary provides subscript on getting value of associated with such key, it reduces to something similar to @Nevin’s function, but OP’s code feel somewhat broken, esp. that you delete all other values in inner loop on succeed.

If you want to print all values in dictionary you can just print(pillTally)

(Ben) #4

Nevin’s solution did work,

For some reason I don’t understand the update subscript for Dictionary will scrub other values, while Nevin’s check for a non-nil value doesn’t? Is it because once the scope is narrowed down to the value of a single key pair, that all other key pairs are ignored?

All of the values in the nested Dictionary default to 0, so I don’t have to worry about a nil return. The point of checking the values was to only print out one set of nested results, so the print option would push everything out and it would look a mess compared to the pretty print option.

Thanks again for both your help!

(Jeremy David Giesbrecht) #5
a.updateValue([b : c], forKey: d) more‐or‐less the same as...

a[d] = [b: c]

You are setting a’s value at d to be a brand‐new dictionary with a single key‐value pair ([b: c]). That wipes out whatever dictionary used to be stored at a[d], no matter how many entries it had.