When is hashValue in swift computed?

234 views Asked by At

Let's say I have a massive String, so hashing it is fairly expensive.

When does the hashValue gets computed? Every time I access it as words.hashValue, or every time the String gets modified? If it's on access, is it cached?

1

There are 1 answers

3
Dávid Pásztor On

As the docs on hashValue state, that requirement on Hashable is deprecated, you should be using hash(into:) instead, which should only be called when you are storing the String as a Hashable, by using it as a Dictionary key or in a Set.

For more info, see SE-0206 Hashable enhancements.