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 dupli
Create a People
struct or class and use instances of that in an array rather than a dictionary:
struct Person {
var age : Int
}
let people = [Person(age: 14), Person(age: 15)] // and so on
for person in people {
print(person)
}
A dictionary is a mapping of a unique key to some value. Therefore what you previously did was not working because your key age
was not unique. You can however use a different dictionary:
let people = [14: Person(age: 14), 15: Person(age: 15)] // and so on
for (key, value) in people {
print("\(key) => \(value)")
}