我的iOS项目一直面临以下问题(这只是一个警告)。
'Hashable.hashValue'作为协议要求被弃用;通过实现'hash(into :)'来使'ActiveType'类型符合'Hashable'
源代码:
public enum ActiveType {
case mention
case hashtag
case url
case custom(pattern: String)
var pattern: String {
switch self {
case .mention: return RegexParser.mentionPattern
case .hashtag: return RegexParser.hashtagPattern
case .url: return RegexParser.urlPattern
case .custom(let regex): return regex
}
}
}
extension ActiveType: Hashable, Equatable {
public var hashValue: Int {
switch self {
case .mention: return -1
case .hashtag: return -2
case .url: return -3
case .custom(let regex): return regex.hashValue
}
}
}
更好的解决方案?警告本身建议我实现'hash(into :)'但我不知道,怎么样?
参考:ActiveLabel
正如警告所说,现在你应该实现hash(into:)
功能。
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
}
}
提示:您不需要使枚举明确符合Equatable
,因为Hashable
扩展了它。