在字典上迭代时如何快速避免重复键错误 [英] How to avoid duplicate key error in swift when iterating over a dictionary

查看:79
本文介绍了在字典上迭代时如何快速避免重复键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在练习快速,并且尝试遍历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

如何清除错误?

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

推荐答案

每个字典键必须唯一

let people = ["age1":14, "age2":15, "age3":75, "age4":43, "age5":103, "age6":87, "age7":12]
for (key, value) in people {
    print(value)
}

这篇关于在字典上迭代时如何快速避免重复键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆