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
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
You can use any type that conforms to the Hashable protocol in a set or as a dictionary key. Many types in the standard library conform to Hashable : Strings, integers, floating-point and Boolean values, and even sets are hashable by default.
In Swift, a Hashable is a protocol that provides a hashValue to our object. The hashValue is used to compare two instances. To use the hashValue , we first have to conform (associate) the type (struct, class, etc) to Hashable property.
A hash value is an integer representation of some other value. In Swift that's a 64-bit integer, which gives quite a bit of room for all your things: just over eighteen quintillion, or 18,446,744,073,709,551,615.
Equatable is also the base protocol for the Hashable and Comparable protocols, which allow more uses of your custom type, such as constructing sets or sorting the elements of a collection. struct Person { var name: String var age: String } 1.
With the implementation of a universal hash function and automatic synthesis feature in the current release of Swift, it has become so convenient to implement the Hashable protocol. Advice for programmers.
In other words, just like other protocols, the Hashable protocol is implemented at the type (e.g., class, struct) level, so that all its instances are hashable.
Another thing to mention is that the Hashable protocol inherits from the Equatable protocol, so that if a data type conforms to Hashable, it has to conform to Equatable as well. What will happen if two instances have the same hashValue?
The standard universal hash function uses a per-execution random 128-bit seed value, making hash values less predictable. More specifically, the universal hash function used in the Swift standard library is SipHash, which was originally designed by Jean-Philippe Aumasson and Daniel J. Bernstein in 2012.
As the warning says, now you should implement the hash(into:)
function instead.
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 } }
It would be even better (in case of enums and struct) to remove the custom hash(into:)
implementation (unless you need a specific implementation) as the compiler will automatically synthesize it for you.
Just make your enum conforming to it:
public enum ActiveType: Hashable { 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 } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With