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

后端 未结 9 1224
清酒与你
清酒与你 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:52

    If you don't mind a bit of inefficiency, you can easily convert your tuple to a string and then use that for the dictionary key...

    var dict = Dictionary() 
    
    let tup = (3,4)
    let key:String = "\(tup)"
    dict[key] = ...
    

提交回复
热议问题