How to elegantly compare tuples in Swift?

后端 未结 6 1845
眼角桃花
眼角桃花 2020-12-09 08:44

I do have 2 different tuples of type (Double, Double):

let tuple1: (Double, Double) = (1, 2)
let tuple2: (Double, Double) = (3, 4)

I want t

6条回答
  •  鱼传尺愫
    2020-12-09 08:53

    Similar to the answer of @JeremyP, but more generic:

    func ==(lhs: (T1, T2), rhs: (T1, T2)) -> Bool {
        return lhs.0 == rhs.0 && lhs.1 == rhs.1
    }
    
    func ==(lhs: (T1, T2, T3), rhs: (T1, T2, T3)) -> Bool {
        return lhs.0 == rhs.0 && lhs.1 == rhs.1 && lhs.2 == rhs.2
    }
    
    func ==(lhs: (T1, T2, T3, T4), rhs: (T1, T2, T3, T4)) -> Bool {
        return lhs.0 == rhs.0 && lhs.1 == rhs.1 && lhs.2 == rhs.2 && lhs.3 == rhs.3
    }
    

提交回复
热议问题