美文网首页
Equatable 和 Hashable 协议的用法

Equatable 和 Hashable 协议的用法

作者: winlee | 来源:发表于2020-03-20 14:00 被阅读0次
    struct Position {
        var x: Int
        var y: Int
        
    }
    
    extension Position: Equatable, Hashable {
        
    }
    
    let availablePositions = [Position(x: 0, y: 0), Position(x: 0, y: 1), Position(x: 1, y: 0)]
    let gemPosition = Position(x: 1, y: 0)
    
    for position in availablePositions {
        if gemPosition == position {
            print("Gem found at (\(position.x), \(position.y))!")
        } else {
            print("No gem at (\(position.x), \(position.y))")
        }
    }
    
    if availablePositions.contains(gemPosition) {
        print("availablePositions 包含 gemPosition")
    } else {
        print("availablePositions 不包含 gemPosition")
    }
    
    class Player {
        var name: String
        var position: Position
        
        init(name: String, position: Position) {
            self.name = name
            self.position = position
        }
    }
    
    // 注意点: == 和 hash(into:) 方法中必须使用相同的属性(名称和数量都要相同)
    extension Player: Equatable {
        static func ==(lhs: Player, rhs: Player) -> Bool {
            return lhs.name == rhs.name && lhs.position == rhs.position
        }
    }
    
    extension Player: Hashable {
        func hash(into hasher: inout Hasher) {
            hasher.combine(name)
            hasher.combine(position)
        }
    }
    
    let p0 = Player(name: "0", position: Position(x: 0, y: 0))
    let p1 = Player(name: "1", position: Position(x: 1, y: 1))
    let p2 = Player(name: "0", position: Position(x: 0, y: 0))
    let ps = [p0, p1, p2]
    
    if p0 == p2 {
        print("p0 = p2")
    }
    
    if p0 != p1 {
        print("p0 != p1")
    }
    
    if ps.contains(p1) {
        print("ps 包含 p1")
    }
    
    class MyNSObjectSubclass: NSObject {
        var name: String
        var position: Position
        
        init(name: String, position: Position) {
            self.name = name
            self.position = position
        }
    }
    // 注意点: isEqual 和 hash 方法中必须使用相同的属性(名称和数量都要相同)
    extension MyNSObjectSubclass {
        override func isEqual(_ object: Any?) -> Bool {
            guard let other = object as? MyNSObjectSubclass
                else { return false }
            return self.name == other.name
                && self.position == other.position
        }
        override var hash: Int {
            var hasher = Hasher()
            hasher.combine(name)
            hasher.combine(position)
            return hasher.finalize()
        }
    }
    
    let obj0 = MyNSObjectSubclass(name: "0", position: Position(x: 0, y: 0))
    let obj1 = MyNSObjectSubclass(name: "1", position: Position(x: 1, y: 1))
    let obj2 = MyNSObjectSubclass(name: "0", position: Position(x: 0, y: 0))
    let objs = [obj0, obj1, obj2]
    
    if obj0 == obj2 {
        print("obj0 = obj2")
    }
    
    if obj0 != obj1 {
        print("obj0 != obj1")
    }
    
    if objs.contains(obj1) {
        print("objs 包含 obj1")
    }
    

    相关文章

      网友评论

          本文标题:Equatable 和 Hashable 协议的用法

          本文链接:https://www.haomeiwen.com/subject/eedjyhtx.html