What is the string.hashValue property in Swift?

Overview

In Swift, hashValue is a string property, used to return the hash value of a string value.

Syntax

string.hashValue

Return value

It returns the hash value of string.

Example

// create some strings
let name = "Theodore"
let language = "Swift"
let emptyString = ""
// get hashValue property
let debugDesc1 = name.hashValue
let debugDesc2 = language.hashValue
let debugDesc3 = emptyString.hashValue
// print results
print(debugDesc1) // "Theodore"
print(debugDesc2) // "Swift"
print(debugDesc3) // ""

Explanation

  • Lines 2–4: We create three string values.
  • Lines 7–9: We use the hashValue property to get the hash values of the strings, and save the results.
  • Lines 12–14: We print the results to the console.

Free Resources