gpt4 book ai didi

arrays - 包含 Int 数组的字典 – 获取键和索引的值

转载 作者:行者123 更新时间:2023-11-30 14:08:37 25 4
gpt4 key购买 nike

我想创建一个映射 Int 的字典到 Int 的数组上s。使用结构var myDictionary = [Int:[Int]]()当我向它添加值时,它似乎工作得很好。但我在检索键和索引的值时遇到问题。我在做myDictionary[key][index] ,但似乎不起作用。我收到错误

Cannot subscript a value of type '[(Int)]?' with an index of type 'Int'

你能解释一下我做错了什么吗?

最佳答案

如果您使用键为 Dictionary 下标,您将获得一个可选值(在本例中为 [Int]?)。如果你想为这个数组添加下标,你必须使用可选链接,因为可选没有这个下标:

let optionalElement: Int? = myDictionary[key]?[index]

关于arrays - 包含 Int 数组的字典 – 获取键和索引的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32034075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com