EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Swift Tutorial Swift hashable
Secondary Sidebar
Swift Tutorial
  • Basics
    • How To Install Swift?
    • Swift Version
    • Swift Package Manager
    • Swift Operators
    • Swift Loop
    • Swift For Loop
    • Swift while loop
    • Swift do-while
    • Swift array
    • Swift Queue
    • Swift Dictionary
    • Swift forms
    • Swift sets
    • Swift map
    • Swift int to string
    • Swift Interview Questions
    • Swift extension
    • Swift guard
    • Swift enum
    • Swift zip
    • Swift?hashable
    • MVVM Swift

Swift hashable

Swift hashable

Introduction to Swift hashable

The swift hashable is the protocol used to compare two objects or instances of the swift class. The swift hashable protocol converts the data type values into hash code or hash value and returns on the output screen. It is a protocol using to conform the Boolean values, string values, and integer values into secure or encrypted values. The hashable protocol is converted data type values into an encrypted integer value and returns on the output window. The hashable protocol is comparing the two objects of the class or struct and returns the object value into integer format or hash code format. The hashable is an easy way to decide the one object is equal, greater, less, or not equal to another object.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

There are three parts of the syntax to become a hashable protocol.

  • Part1: the Hashable keyword use with struct or class.

class ClassName: Hashable {
//defined variable here…
}

OR

struct StructName: Hashable {
//defined variable here…
}

  • Part2: the variable value converts into hash value.

public var hashValue: Int {
return variable_name.hashValue
}

  • Part3: the object value returns into hash value or hash code.

print(object_name.hashValue)

Description:

  • The hashable part2 syntax is placed inside of the class or struct.
  • The hashable provides equitable and comparable protocols with hashcode.
  • The hashValue keyword displays the hash code, hash value, or integer value.
  • If two objects have similar values, then their hash value must be the same.
  • If two hash codes or values have the same value, then two objects do not seem necessary to have similar values.

How does hashable works in Swift?

The swift installation for the swift enum programming.

  • The Ios and OS x users have installed Xcode IDE, but the user must have an account on the apple developer website.
  • Users enter the playground name and select the platform name in the Xcode IDE.
  • Users can get the playground window and start the coding.
  • The other operating system users can use online options for swift programming.
  • The user can use the https://iswift.org/playground website for swift coding.

The swift hashable create a class or struct with the “Hashable” keyword.

The variable and its datatype are defines inside of swift class or struct.

class Employee: Hashable {
var employee_id: Int
var employee_age: Int
}
The swift hashable convert variable value into hash value.
public var hashValue: Int {
return employee_id.hashValue ^ employee_age.hashValue
}

The swift hashable create an initialize variable method as a constructor.

init(employee_id: Int, employee_age: Int) {
self.employee_id = employee_id
self.employee_age = employee_age
}

The swift hashable protocol supports the equatable protocol and uses the method for comparison.

static func == (lhs: Employee, rhs: Employee) -> Bool {
return lhs.employee_id == rhs.employee_id && lhs.employee_age == rhs.employee_age
}

Defined objects value and Return the hashable value.

let employee1 = Employee(employee_id: 1,  employee_age: 8)
print(employee1.hashValue)

Combine the working procedure of the hashable protocol is below.

class Employee: Hashable {
var employee_id: Int
var employee_age: Int
public var hashValue: Int {
return employee_id.hashValue ^ employee_age.hashValue
}
init(employee_id: Int, employee_age: Int) {
self.employee_id = employee_id
self.employee_age = employee_age
}
static func == (lhs: Employee, rhs: Employee) -> Bool {
return lhs.employee_id == rhs.employee_id && lhs.employee_age == rhs.employee_age
}
}
let employee1 = Employee(employee_id: 1,  employee_age: 3)
print(employee1.hashValue)

Examples

Different examples are mentioned below:

Example #1

The swift hashable with similar object value using struct example and output.

Code:

class Employee: Hashable {
var employee_id: Int
var salary: Int
var employee_age: Int
public var hashValue: Int {
return employee_id.hashValue ^ employee_age.hashValue ^ salary.hashValue
}
init(employee_id: Int, salary: Int, employee_age: Int) {
self.employee_id = employee_id
self.salary = salary
self.employee_age = employee_age
}
static func == (lhs: Employee, rhs: Employee) -> Bool {
return lhs.employee_id == rhs.employee_id
&& lhs.employee_age == rhs.employee_age
&& lhs.salary == rhs.salary
}
}
let employee1 = Employee(employee_id: 1, salary: 8, employee_age: 3)
let employee2 = Employee(employee_id: 1, salary:  8, employee_age: 3)
print(employee1.hashValue)
print(employee2.hashValue)

Output:

Swift hashable output 1

Example #2

The hashable with different object values using struct example and output.

Code:

struct Employee: Hashable {
var employee_id: Int
var salary: Int
var employee_age: Int
public var hashValue: Int {
return employee_id.hashValue ^ employee_age.hashValue ^ salary.hashValue
}
init(employee_id: Int, salary: Int, employee_age: Int) {
self.employee_id = employee_id
self.salary = salary
self.employee_age = employee_age
}
static func == (lhs: Employee, rhs: Employee) -> Bool {
return lhs.employee_id == rhs.employee_id
&& lhs.employee_age == rhs.employee_age
&& lhs.salary == rhs.salary
}
}
let employee1 = Employee(employee_id: 1, salary: 18000, employee_age: 31)
let employee2 = Employee(employee_id: 3, salary:  205400, employee_age: 38)
print("The employee1 value into hashable formate : ")
print(employee1.hashValue)
print("The employee2 value into hashable formate : ")
print(employee2.hashValue)

Output:

Swift hashable output 2

Example #3

Swift hashable with comparison objects using struct example and output.

Code:

struct Employee: Hashable {
var employee_id: Int
var salary: Int
var employee_age: Int
public var hashValue: Int {
return employee_id.hashValue ^ employee_age.hashValue ^ salary.hashValue
}
init(employee_id: Int, salary: Int, employee_age: Int) {
self.employee_id = employee_id
self.salary = salary
self.employee_age = employee_age
}
static func == (lhs: Employee, rhs: Employee) -> Bool {
return lhs.employee_id == rhs.employee_id
&& lhs.employee_age == rhs.employee_age
&& lhs.salary == rhs.salary
}
}
let employee1 = Employee(employee_id: 1, salary: 18000, employee_age: 31)
let employee2 = Employee(employee_id: 3, salary:  205800, employee_age: 38)
if(employee1 == employee2){
print("employee1 == employee2")
}else{
print("employee1 != employee2")
}

Output:

output 3

Example #4

The hashable with similar object value except employee id considered example and output.

Code:

class Employee: Hashable {
var employee_id: Int
var salary: Int
var employee_age: Int
public var hashValue: Int {
return employee_age.hashValue ^ salary.hashValue
}
init(employee_id: Int, salary: Int, employee_age: Int) {
self.employee_id = employee_id
self.salary = salary
self.employee_age = employee_age
}
static func == (lhs: Employee, rhs: Employee) -> Bool {
return lhs.employee_age == rhs.employee_age
&& lhs.salary == rhs.salary
}
}
let employee1 = Employee(employee_id: 1, salary: 204823, employee_age: 3)
let employee2 = Employee(employee_id: 2, salary:  204823, employee_age: 3)
print(employee1.hashValue)
print(employee2.hashValue)
if(employee1 == employee2){
print("employee1 == employee2")
}else{
print("employee1 != employee2")
}

Output:

output 4

Conclusion

It is a secure integer value help to the comparison between two objects. The hashable is an easy and user-friendly way to compare instances in the Boolean format.

Recommended Articles

This is a guide to Swift hashable. Here we discuss How does hashable works in Swift and Examples along with the codes snd outputs. You may also have a look at the following articles to learn more –

  1. Swift Version
  2. Swift For Loop
  3. What is Swift?
  4. Swift Interview Questions
Popular Course in this category
Swift Training (10 Courses, 11+ Projects)
  10 Online Courses |  11 Hands-on Projects |  63+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Windows Forms Training (4 Courses, 3+ Projects)4.9
Kali Linux Training (3 Courses, 3+ Projects)4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more