swfit 中的Hashable 和 Equatable 的使用
final class WebSocketClient: Hashable, Equatable {//继承哈希协议, 才能放进去set集合
let id: String
let connection: NWConnection
init(connection: NWConnection) {
self.connection = connection
self.id = UUID().uuidString
}
// MARK:- 判断等价的条件 Equatable协议
//Returns a Boolean value indicating whether two values are equal.
static func == (lhs: WebSocketClient, rhs: WebSocketClient) -> Bool {
lhs.id == rhs.id
}
// MARK:- 提供一个哈希标识
/// Hashes the essential components of this value by feeding them into the
/// given hasher.
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
本文标题:swfit 中的Hashable 和 Equatable 的使用
本文链接:https://www.haomeiwen.com/subject/tuvynhtx.html
网友评论