Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

hash
string
swift
communitycreator

What is hashValue String property in Swift?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

We can use the hashValue on a string to return the hash value of the string. It is a String instance property.

Note: Hash values are not guaranteed to be equal when they are executed at different times.

Syntax

string.hashValue

Return value

An integer is returned, which is the hash value of string.

Example

Let’s look at the code below:

// import Foundation framework
import Foundation
// create some strings
let str1 = "Edpresso"
let str2 = "is"
let str3 = "the"
let str4 = "best!"
// get hash values and print results
print(str1.hashValue)
print(str2.hashValue)
print(str3.hashValue)
print(str4.hashValue)
Getting hash values of some Strings

Explanation

  • Line 2: We import the Foundation framework. The Foundation framework allows us to use the hashValue property of a String instance and lots more.
  • Lines 5 to 8: We use these lines to create some strings.
  • Lines 11 to 14: We use str.hashValue to return the hash of a string. Hash values of the strings are created, and the values are printed to the console.

RELATED TAGS

hash
string
swift
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring