Make struct Hashable? Make struct Hashable? swift swift

Make struct Hashable?


Simply return dbName.hashValue from your hashValue function. FYI - the hash value does not need to be unique. The requirement is that two objects that equate equal must also have the same hash value.

struct PetInfo: Hashable {    var petName: String    var dbName: String    var hashValue: Int {        return dbName.hashValue    }    static func == (lhs: PetInfo, rhs: PetInfo) -> Bool {        return lhs.dbName == rhs.dbName && lhs.petName == rhs.petName    }}


As of Swift 5 var hashValue:Int has been deprecated in favour of func hash(into hasher: inout Hasher) (introduced in Swift 4.2), so to update the answer @rmaddy gave use:

func hash(into hasher: inout Hasher) {    hasher.combine(dbName)}