比较名为元组的Swift数组

时间:2016-02-27 05:13:23

标签: swift tuples

我试图比较两个元组数组。我可以得到我想要比较单个元组实例的工作,但不是数组版本。我在下面的代码中出了什么问题?它能以更简洁的方式完成吗?

目前我得到"二元运算符!=不能应用于两个' [(sort:Double,name:String,circleImageURLString:String?)]'操作数"

func != <T0:Equatable, T1:Equatable, T2:Equatable> (tuple1:(sort:T0, name:T1, circleImageURLString:T2), tuple2:(sort:T0, name:T1, circleImageURLString:T2)) -> Bool {
    return tuple1.0 != tuple2.0 || tuple1.1 != tuple2.1 || tuple1.2 != tuple2.2
}

func != <T0:Equatable, T1:Equatable, T2:Equatable> (array1:[(sort:T0, name:T1, circleImageURLString:T2)], array2:[(sort:T0, name:T1, circleImageURLString:T2)]) -> Bool {

    if array1.count != array2.count {
        return true
    }

    for (index, value) in array1.enumerate() {
        if value != array2[index] {
            return true
        }
    }

    return false
}

class myGreatTest {

    func test() {

        let array1: [(sort: Double, name: String, circleImageURLString: String?)] = [(sort:12.34, name:"Test 1", circleImageURLString:"www.test.com/1.png")]
        let array2: [(sort: Double, name: String, circleImageURLString: String?)] = [(sort:56.78, name:"Test 2", circleImageURLString:nil)]

        if array1 != array2 {
            print("Not equal")
        }

    }

}

1 个答案:

答案 0 :(得分:-4)

显然现在Swift 2.2本身支持它。