我试图创建一个字典,把键作为我创建的结构体,把值作为一个整数数组,但是,我总是得到错误:
类型“DateStruct”不符合协议“哈希"
我确信我已经实现了必要的方法,但由于某种原因,它仍然不起作用。
下面是我的结构和实现的协议:
struct DateStruct {
var year: Int
var month: Int
var day: Int
var hashValue: Int {
return (year+month+day).hashValue
}
static func == (lhs: DateStruct, rhs: DateStruct) -> Bool {
return lhs.hashValue == rhs.hashValue
}
static func < (lhs: DateStruct, rhs: DateStruct) -> Bool {
if (lhs.year < rhs.year) {
return true
} else if (lhs.year > rhs.year) {
return false
} else {
if (lhs.month < rhs.month) {
return true
} else if (lhs.month > rhs.month) {
return false
} else {
if (lhs.day < rhs.day) {
return true
} else {
return false
}
}
}
}
}
有人能解释一下为什么我还是会收到错误吗?
7条答案
按热度按时间xwbd5t1u1#
你忘了宣言:
而且你的
==
函数是错误的,你应该比较一下这三个属性。两个不同的值可能具有相同的哈希值。
ne5o7dgx2#
当
仍然适用于旧版NSObject继承树,否则将被废弃。
是在课堂上快速而现代地乱讲的方法。
根据上面的rmaddy答案,“==”操作符也必须解决一些问题,才能符合您的语义。
根据Manish的说法,你可以免费获得结构体的哈希一致性,只要声明一下。
v09wglhw3#
如果类具有类型的字段(另一个类),则该类应该采用哈希表。
范例
这里,Activity类也必须采用哈希表。
kq0g1dla4#
如果不想使用hashValue,可以将值的散列与
hash(into:)
方法结合使用。有关详细信息,请参阅答案:https://stackoverflow.com/a/55118328/1261547
gv8xihay5#
对于简单的结构体,它的所有属性都已经是
Hashable
(即Int
,String
,...),我们只需声明它就可以符合Hashable
(参见https://developer.apple.com/documentation/swift/hashable)所以不需要实现
hashValue
(顺便说一句,它已经过时了),也不需要实现==
(因为Hashable
符合Equatable
)。既然我们要实现
<
操作符,那么遵循Comparable
是有意义的,这样我们就可以排序(即[dateStructA, dateStructB, ...].sorted()
)。所以我会这样做:
zu0ti5jz6#
定义结构时未指定哈希协议:
struct DateStruct: Hashable { ...
下面的代码来自你的例子,它运行在一个Playground上。请注意你的==运算符在这里被修改了:
tcomlyy67#