Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: 'Hashable.hashValue' is deprecated as a protocol requirement;

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         }     } } 

enter image description here

Any better solution? The warning itself suggesting me to implement 'hash(into:)' but I don't know, how?

Reference: ActiveLabel

like image 492
Krunal Avatar asked Mar 28 '19 10:03

Krunal


People also ask

Can a protocol conform to hashable?

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.

What is the use of hashable protocol in Swift?

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.

What is hashValue in Swift?

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.

What is Equatable and hashable in Swift?

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.

Is it possible to implement HASHABLE protocol in Swift?

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.

What is the HASHABLE protocol?

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.

What is the difference between equatable and 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?

What is the universal hash function in Swift?

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.


1 Answers

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         }     } } 
like image 155
Rico Crescenzio Avatar answered Sep 23 '22 15:09

Rico Crescenzio