加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Swift中,我可以使用元组作为字典中的键吗?

发布时间:2020-12-14 06:00:21 所属栏目:百科 来源:网络整理
导读:我想知道如果我能以某种方式使用x,y对作为我的字典的关键 let activeSquares = Dictionary (x: Int,y: Int),SKShapeNode() 但我得到的错误: Cannot convert the expression's type 'error type' to type '$T1' 和错误: Type '(x: Int,y: Int)?' does not
我想知道如果我能以某种方式使用x,y对作为我的字典的关键
let activeSquares = Dictionary <(x: Int,y: Int),SKShapeNode>()

但我得到的错误:

Cannot convert the expression's type '<<error type>>' to type '$T1'

和错误:

Type '(x: Int,y: Int)?' does not conform to protocol 'Hashable'

那么,我们如何使它符合?

Dictionary的定义是struct Dictionary< KeyType:Hashable,ValueType> :…,即密钥的类型必须符合协议Hashable。但语言指南告诉我们 protocols can be adopted by classes,structs and enums,即不是元组。因此,元组不能用作字典键。

解决方法是定义一个包含两个Ints(或任何你想放在你的元组中)的哈希表结构类型。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读