How to elegantly compare tuples in Swift?

后端 未结 6 1838
眼角桃花
眼角桃花 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 09:11

    Swift 4 supports tuple comparison. You won't get the error anymore.

    This code runs perfectly

    let tuple1 : (Double, Double) = (1,2)
    let tuple2 : (Double, Double) = (3,4)
    
    if (tuple1 == tuple2) {
        print("equal")
    }
    else {
        print("unequal")
    }
    

    Here, unequal is printed in the console of the playground.

    One limitation in tuple comparison as mentioned in the apple doc is -

    The Swift standard library includes tuple comparison operators for tuples with fewer than seven elements. To compare tuples with seven or more elements, you must implement the comparison operators yourself.

提交回复
热议问题