我不太清楚如何处理编译器发出的不使用警告hashValue
而是实施hash(into:)
.
“Hashable.hashValue”作为协议要求已被弃用;符合
通过实现“hash(into:)”将“MenuItem”键入“Hashable”
答案来自Swift:“Hashable.hashValue”作为协议要求已被弃用; https://stackoverflow.com/questions/55395207/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
}
}
我确实有这个结构,可以自定义PagingItem
羊皮纸 (https://github.com/rechsteiner/Parchment 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
}
}
你可以简单地使用hasher.combine
并使用您想要用于散列的值来调用它:
func hash(into hasher: inout Hasher) {
hasher.combine(index)
hasher.combine(title)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)