How do I check if an array of tuples contains a particular one in Swift?

前端 未结 6 964
温柔的废话
温柔的废话 2021-01-12 02:28

Consider the following Swift code.

var a = [(1, 1)]

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

All I need is to check if a

6条回答
  •  孤街浪徒
    2021-01-12 02:47

    Add the following to your code:

    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 is not that flexible when it comes to tuples. They do not conform to the Equatable protocol. So you must define that or use the above function.

提交回复
热议问题