美文网首页
swfit 中的Hashable 和 Equatable 的使用

swfit 中的Hashable 和 Equatable 的使用

作者: coder_xiang | 来源:发表于2020-05-11 16:02 被阅读0次
    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