Conforming to Hashable protocol? Conforming to Hashable protocol? swift swift

Conforming to Hashable protocol?


You're missing the declaration:

struct DateStruct: Hashable {

And your == function is wrong. You should compare the three properties.

static func == (lhs: DateStruct, rhs: DateStruct) -> Bool {    return lhs.year == rhs.year && lhs.month == rhs.month && lhs.day == rhs.day}

It's possible for two different values to have the same hash value.


Btw

var hashValue: Int 

is obsolete (except in the legacy NSObject inheritance trees).

    func hash(into hasher: inout Hasher)    {        hasher.combine(year);        hasher.combine(month)     ...

is the new way


If you don't want to use the hashValue, you can combine the hash of your values with the hash(into:) method.

For more information see the answer : https://stackoverflow.com/a/55118328/1261547