我对编译器发出的废弃警告,不再使用hashValue
而是实现hash(into:)
,并没有很清楚的想法。
'Hashable.hashValue'已被弃用作为协议要求; 通过实现'hash(into:)'将类型'MenuItem'符合'Hashable'
Swift: 'Hashable.hashValue' is deprecated as a protocol requirement; 的答案有以下示例:
func hash(into hasher: inout Hasher) {
switch self {
case .mention: hasher.combine(-1)
case .hashtag: hasher.combine(-2)
case .url: hasher.combine(-3)
case .custom(let regex): hasher.combine(regex) // assuming regex is a string, that already conforms to hashable
}
}
我有这个结构体,可以自定义 Parchment 的 PagingItem
(https://github.com/rechsteiner/Parchment)。
import Foundation
/// The PagingItem for Menus.
struct MenuItem: PagingItem, Hashable, Comparable {
let index: Int
let title: String
let menus: Menus
var hashValue: Int {
return index.hashValue &+ title.hashValue
}
func hash(into hasher: inout Hasher) {
// Help here?
}
static func ==(lhs: MenuItem, rhs: MenuItem) -> Bool {
return lhs.index == rhs.index && lhs.title == rhs.title
}
static func <(lhs: MenuItem, rhs: MenuItem) -> Bool {
return lhs.index < rhs.index
}
}