迭代字典时如何避免swift中的重复键错误

时间:2022-11-06 22:24:18

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

我正在练习swift,我正在尝试迭代一个字典来打印密钥,但它给了我一个

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)
}

3 个解决方案

#1


7  

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)
}

#2


2  

Create a People struct or class and use instances of that in an array rather than a dictionary:

创建一个People结构或类,并在数组中使用它的实例而不是字典:

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)")
}

#3


1  

As others already said, you cannot create a dictionary where the same key does appear more then once.

正如其他人已经说过的那样,你不能创建一个字典,其中相同的键确实出现过一次。

That said I really like solution provided by luk2302 because if does offer a well structured approach.

那说我真的很喜欢luk2302提供的解决方案,因为如果确实提供了一个结构良好的方法。

Here I am just adding another solution.

我在这里只是添加另一个解决方案。

Since the real information in your (wrong) dictionary is the value (not the key) what's the meaning of using a Dictionary?

由于您(错误)字典中的真实信息是值(而不是键),使用字典的含义是什么?

You could simply use an array

你可以简单地使用一个数组

let ages = [14, 15, 75, 43, 103, 87, 12]
ages.forEach { print($0) }

#1


7  

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)
}

#2


2  

Create a People struct or class and use instances of that in an array rather than a dictionary:

创建一个People结构或类,并在数组中使用它的实例而不是字典:

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)")
}

#3


1  

As others already said, you cannot create a dictionary where the same key does appear more then once.

正如其他人已经说过的那样,你不能创建一个字典,其中相同的键确实出现过一次。

That said I really like solution provided by luk2302 because if does offer a well structured approach.

那说我真的很喜欢luk2302提供的解决方案,因为如果确实提供了一个结构良好的方法。

Here I am just adding another solution.

我在这里只是添加另一个解决方案。

Since the real information in your (wrong) dictionary is the value (not the key) what's the meaning of using a Dictionary?

由于您(错误)字典中的真实信息是值(而不是键),使用字典的含义是什么?

You could simply use an array

你可以简单地使用一个数组

let ages = [14, 15, 75, 43, 103, 87, 12]
ages.forEach { print($0) }