Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift
hash
string
communitycreator

What is string.hash 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

The hash property of a string returns an unsigned integer that can be used as a hash address.

Syntax

string.hash

Return value

This value returns an unsigned integer value.

Example

Let’s look at the code below:

// import Foundation
import Foundation
// create string values
let name = "Edpresso"
let role = "Learning Platform"
let author = "Theodore"
let badge = "Gold"
// get hash and print results
print(name.hash)
print(role.hash)
print(author.hash)
print(badge.hash)
Getting hash of a String value in Swift

Explanation

  • Line 2: We import the Foundation framework. It allows us to access a String instance’s hash property and lots more.
  • Lines 5 to 8: We create some string values.
  • Lines 11 to 14: We get the hash of each string using the hash property and the results are printed to the console.

RELATED TAGS

swift
hash
string
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