Your first issue is that value
is a let
constant within your loop body. You must declare it as var
in order to mutate it.
Your second issue is that you're trying to use value.append(nameText)
as the value to set for the key. However, append()
mutates the array in place, and returns Void
.
Thirdly, don't use updateValue(forKey:)
. There's really no point. Use subscripting instead.
var dictionary = [
"a" : ["a"],
"b" : ["b", "b"],
"c" : ["c", "c", "c"],
]
let nameText = "foo"
for (key, var value) in dictionary {
value.append(nameText)
dictionary["name"] = value
}
Now, this gets your code to compile, but I'm highly skeptical this is really what you want to do. You'll be overwriting the value for the "name"
key on every iteration, meaning only the last iteration's value will persist. Furthermore, because Dictionary
doesn't have a defined ordering, this code has indeterminate behaviour. What are you actually trying to do?