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

前端 未结 6 1010
温柔的废话
温柔的废话 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:55

    Maybe too old for this question hope someone will get help with more option.

    You may use switch instead of if condition

        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")
            }
        }
    

提交回复
热议问题