e_mam106 e_mam106 - 4 years ago 836
Swift Question

How to avoid duplicate key error in swift when iterating over a dictionary

I'm practicing swift and I'm trying to iterate over a Dictionary to print the key, but it gives me a


fatal error: Dictionary literal contains duplicate keys


How can remove the error?

let people = ["age":14, "age":15, "age":75, "age":43, "age":103, "age":87, "age":12]
for (key, value) in people {
print(value)
}

Answer Source

Each dictionary key MUST be unique

let people = ["age1":14, "age2":15, "age3":75, "age4":43, "age5":103, "age6":87, "age7":12]
for (key, value) in people {
    print(value)
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download