I've been facing following issue (it's just a warning) with my iOS project.
'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'ActiveType' to 'Hashable' by implementing 'hash(into:)' instead
- Xcode 10.2
- Swift 5
Source Code:
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
}
}
}
Any better solution? The warning itself suggesting me to implement 'hash(into:)' but I don't know, how?
Reference: ActiveLabel
Hashable
conformance for yourenum
. You also don't need to explicitly stateEquatable
conformance, sinceHashable
inherits fromEquatable
, so when you declareHashable
conformance,Equatable
methods are synthetised for you. – LitotesHashable
moved away from asking conforming types for ahashValue: Int
that describes themselves, to asking them to take in aHasher
, and "mix" themselves into it (by mixing in their fields). Previously people had difficulty deriving good hash values for objects with multiple fields, often resorting to hacks, like XORing all the elements (a ^ b ^ c
), or worse, taking the string value of a string that concatinates the elements ("\(a)-\(b)-\(c)".hashValue
). Now instead, you just tell the hasher what to hash, and it uses a proper hashing algorithm to do that on your behalf. – Dialectal