如何在Swift中检查元组数组是否包含特定元组?

时间:2015-04-19 21:42:12

标签: arrays swift tuples

考虑以下Swift代码。

var a = [(1, 1)]

if contains(a, (1, 2)) {
    println("Yes")
}

我只需检查a是否包含元组,但代码会导致错误。

  

无法找到'包含'接受参数列表   类型'([(Int,Int)],(Int,Int))'

为什么如此以及如何正确使用contains

6 个答案:

答案 0 :(得分:9)

虽然元组不是Equatable,但您不需要编写自己的contains版本,因为有contains版本需要匹配的谓词:

if contains(a, { $0.0 == 1 && $0.1 == 2 }) {
     // a contained (1,2)
}

虽然你不能将元组扩展为等价,但可以为元组编写==版本,这将使上述代码更简单:

func ==<T: Equatable, U: Equatable>(lhs: (T,U), rhs: (T,U)) -> Bool {
    return lhs.0 == rhs.0 && lhs.1 == rhs.1
}

contains(a) { $0 == (1,2) } // returns true

很好能够为元组编写contains版本,但是,我不认为占位符语法支持它:

编辑:从Swift 1.2开始,现在可以编译,因为你可以在占位符约束中使用元组

func contains
  <S: SequenceType, T: Equatable, U: Equatable where S.Generator.Element == (T,U)>
  (seq: S, x: (T,U)) -> Bool {
    return contains(seq) { $0.0 == x.0 && $0.1 == x.1 }
}

let a = [(1,1), (1,2)]

if contains(a, (1,2)) {
    println("Yes")
}

答案 1 :(得分:8)

Xcode 8.2.1•Swift 3.0.2

let tuples = [(1, 1), (0, 1)]

let tuple1 = (1, 2)
let tuple2 = (0, 1)

if tuples.contains(where: {$0 == tuple1}) {
    print(true)
} else {
    print(false)    // false
}

if tuples.contains(where: {$0 == tuple2}) {
    print(true)    // true
} else {
    print(false)
}

答案 2 :(得分:6)

将以下内容添加到您的代码中:

func contains(a:[(Int, Int)], v:(Int,Int)) -> Bool {
  let (c1, c2) = v
  for (v1, v2) in a { if v1 == c1 && v2 == c2 { return true } }
  return false
}

当谈到元组时,Swift并不灵活。它们不符合Equatable协议。所以你必须定义它或使用上面的函数。

答案 3 :(得分:2)

您无法使用contains方法解决问题。 Swift中也没有嵌入式解决方案。所以你需要自己解决这个问题。 您可以创建一个简单的函数来检查数组中的元组是否与要检查的元组相同:

func checkTuple(tupleToCheck:(Int, Int), theTupleArray:[(Int, Int)]) -> Bool{
    //Iterate over your Array of tuples
    for arrayObject in theTupleArray{
        //If a tuple is the same as your tuple to check, it returns true and ends
        if arrayObject.0 == tupleToCheck.1 && arrayObject.1 == tupleToCheck.1 {
            return true
        }
    }

    //If no tuple matches, it returns false
    return false
}

答案 4 :(得分:1)

对于这个问题可能太老了,希望有人会得到更多选择的帮助。

您可以使用switch代替if条件

    var somePoint = [(0, 1), (1, 0), (0, 0), (-2, 2)]
    for innerSomePoint in somePoint {
        switch innerSomePoint {
        case (0, 0):
            print("\(innerSomePoint) first and second static")
        case (_, 0):
            print("\(innerSomePoint) first dynamic second static")
        case (0, _):
            print("\(innerSomePoint) first static second dynamic")
        case (-2...2, -2...2):
            print("\(innerSomePoint) both in between values")
        default:
            print("\(innerSomePoint) Nothing found")
        }
    }

Also have some more option to do check here from apple doc

    somePoint = [(1, 1), (1, -1), (0, 0), (-2, 2)]
    for innerSomePoint in somePoint {
        switch innerSomePoint {
        case let (x, y) where x == y:
            print("(\(x), \(y)) is on the line x == y")
        case let (x, y) where x == -y:
            print("(\(x), \(y)) is on the line x == -y")
        case let (x, y):
            print("(\(x), \(y)) is just some arbitrary point")
        }
    }

答案 5 :(得分:0)

快捷键4

将您的代码更改为:

var a = [(1, 1)]

if a.contains(where: { $0 == (1, 2) } ) {
    print("Yes")
}