I've this code that I made on playground to represent my problem:
import Foundation
var countries = ["Poland":["Warsaw":"foo"],"England":["London":"foo"]]
for (country, city) in countries {
if city["London"] != nil {
city["London"] = "Piccadilly Circus" // error because by default the variables [country and city] are constants (let)
}
}
Does anyone know a work around or the best way to make this work?
You can make city
mutable by adding var
to its declaration:
for (country, var city) in countries {
Unfortunately, changing it won't affect your countries
Dictionary, because you're getting a copy of each sub-Dictionary. To do what you want, you'll need to loop through the keys of countries
and change things from there:
for country in countries.keys {
if countries[country]!["London"] != nil {
countries[country]!["London"]! = "Picadilly Circus"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With