The word Hashable in SwiftUI could be scary for some fresh beginners. Trust me it was scary for me when I started programming first.
Let's take a look at an example
This struct conforms to Hashable proctocol.
The above statement does not make sense at first time. You need to understand it like below
This struct let's you compare it's instances.
The above statement means, if you create objects from this class, you would be able to compare and know if your objects are same or not. That's as simple as it is.
Hashable
?
What does it actually do? Actually it gives unique id to your class instances. If you have unique id, then you can compare to objects of the class.
When do you need them?
If you have dynamic objects or events like button clicks, then based on button clicks if you need to response to something else, then you need to Hashable
in your code.
Or if you have NavigationStack
and you need to click on links and go to new page or come back then you need Hashable
.
Create Hashable
To create Hashable struct, all you need to do, add Hashable after your struct.
struct Person:Hashable{
var name:String,
var age: int,
}