class User: Mappable, CustomStringConvertible, Hashable{
static func == (lhs: WUser, rhs: WUser) -> Bool {
return lhs.name == rhs.name
}
var name: String?
var email: String?
.
.
}
Where Mappable is
protocol Mappable: Codable {
init?(jsonData: Data?)
init?(jsonString: String)
}
But it Says, Type 'User' does not conform to protocol 'Hashable'
Remember when you conform your type to a protocol
, you need to implement the required entities(properties, methods) of that protocol
. For example, Hashable has the required method hash(into hasher: inout Hasher)
so you need to implement that as below,
class WUser: Mappable, CustomStringConvertible, Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(self.name)
hasher.combine(self.email)
}
required init?(jsonData: Data?) {
}
required init?(jsonString: String) {
}
var description: String {
return self.name ?? ""
}
static func == (lhs: WUser, rhs: WUser) -> Bool {
return lhs.name == rhs.name
}
var name: String?
var email: String?
}
Above code has no compiling issues as i have implemented all the requirements for each protocol
(i.e, Mappable
, CustomStringConvertible
, Hashable
)