In Swift can I use a tuple as the key in a dictionary?

后端 未结 9 1195
清酒与你
清酒与你 2020-12-03 04:15

I\'m wondering if I can somehow use an x, y pair as the key to my dictionary

let activeSquares = Dictionary <(x: Int, y: Int), SKShapeNode>()
         


        
9条回答
  •  既然无缘
    2020-12-03 04:44

    No need special code or magic numbers to implement Hashable

    Hashable in Swift 4.2:

    struct PairKey: Hashable {
    
        let first: UInt
        let second: UInt
    
        func hash(into hasher: inout Hasher) {
            hasher.combine(self.first)
            hasher.combine(self.second)
        }
    
        static func ==(lhs: PairKey, rhs: PairKey) -> Bool {
            return lhs.first == rhs.first && lhs.second == rhs.second
        }
    }
    

    More info: https://nshipster.com/hashable/

提交回复
热议问题