swfit 中的Hashable 和 Equatable 的使用

2020-05-11  本文已影响0人  coder_xiang
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)
    }
}
上一篇下一篇

猜你喜欢

热点阅读