如何检查元组数组是否包含 Swift 中的特定元组?
How do I check if an array of tuples contains a particular one in Swift?
考虑以下 Swift 代码。
var a = [(1, 1)]
if contains(a, (1, 2)) {
println("Yes")
}
我只需要检查 a
是否包含元组,但代码会导致错误。
Cannot find an overload for 'contains' that accepts an argument list
of type '([(Int, Int)], (Int, Int))'
为什么会这样以及如何正确使用 contains
?
将以下内容添加到您的代码中:
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
协议。所以你必须定义它或使用上面的函数。
您不能使用 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
}
您可以使用谓词并检查是否相等:
let tuples = [(1, 1), (0, 1)]
let tuple1 = (1, 2)
let tuple2 = (0, 1)
if tuples.contains(where: {[=10=] == tuple1}) {
print(true)
} else {
print(false) // false
}
if tuples.contains(where: {[=10=] == tuple2}) {
print(true) // true
} else {
print(false)
}
您还可以创建自己的包含通用元组的方法:
extension Sequence {
func contains<T, U>(_ tuple: (T, U)) -> Bool where T: Equatable, U: Equatable, Element == (T,U) {
contains { [=11=] == tuple }
}
func contains<T, U, V>(_ tuple: (T, U, V)) -> Bool where T: Equatable, U: Equatable, V: Equatable, Element == (T,U,V) {
contains { [=11=] == tuple }
}
func contains<T, U, V, W>(_ tuple: (T, U, V, W)) -> Bool where T: Equatable, U: Equatable, V: Equatable, W: Equatable,Element == (T, U, V, W) {
contains { [=11=] == tuple }
}
func contains<T, U, V, W, X>(_ tuple: (T, U, V, W, X)) -> Bool where T: Equatable, U: Equatable, V: Equatable, W: Equatable, X: Equatable, Element == (T, U, V, W, X) {
contains { [=11=] == tuple }
}
func contains<T, U, V, W, X, Y>(_ tuple: (T, U, V, W, X, Y)) -> Bool where T: Equatable, U: Equatable, V: Equatable, W: Equatable, X: Equatable, Y: Equatable, Element == (T, U, V, W, X, Y) {
contains { [=11=] == tuple }
}
}
if tuples.contains(tuple1) {
print(true)
} else {
print(false) // false
}
if tuples.contains(tuple2) {
print(true) // true
} else {
print(false)
}
虽然元组不是 Equatable
,但您无需编写自己的 contains
版本,因为有一个 contains
版本需要匹配谓词:
if contains(a, { [=10=].0 == 1 && [=10=].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) { [=11=] == (1,2) } // returns true
能够为元组编写 contains
的版本是 nice,但是唉,我认为占位符语法不支持它:
编辑:从 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) { [=12=].0 == x.0 && [=12=].1 == x.1 }
}
let a = [(1,1), (1,2)]
if contains(a, (1,2)) {
println("Yes")
}
这个问题可能太老了希望有人能得到更多选择的帮助。
您可以使用 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")
}
}
Swift 4
将您的代码更改为:
var a = [(1, 1)]
if a.contains(where: { [=10=] == (1, 2) } ) {
print("Yes")
}
考虑以下 Swift 代码。
var a = [(1, 1)]
if contains(a, (1, 2)) {
println("Yes")
}
我只需要检查 a
是否包含元组,但代码会导致错误。
Cannot find an overload for 'contains' that accepts an argument list of type '([(Int, Int)], (Int, Int))'
为什么会这样以及如何正确使用 contains
?
将以下内容添加到您的代码中:
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
协议。所以你必须定义它或使用上面的函数。
您不能使用 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
}
您可以使用谓词并检查是否相等:
let tuples = [(1, 1), (0, 1)]
let tuple1 = (1, 2)
let tuple2 = (0, 1)
if tuples.contains(where: {[=10=] == tuple1}) {
print(true)
} else {
print(false) // false
}
if tuples.contains(where: {[=10=] == tuple2}) {
print(true) // true
} else {
print(false)
}
您还可以创建自己的包含通用元组的方法:
extension Sequence {
func contains<T, U>(_ tuple: (T, U)) -> Bool where T: Equatable, U: Equatable, Element == (T,U) {
contains { [=11=] == tuple }
}
func contains<T, U, V>(_ tuple: (T, U, V)) -> Bool where T: Equatable, U: Equatable, V: Equatable, Element == (T,U,V) {
contains { [=11=] == tuple }
}
func contains<T, U, V, W>(_ tuple: (T, U, V, W)) -> Bool where T: Equatable, U: Equatable, V: Equatable, W: Equatable,Element == (T, U, V, W) {
contains { [=11=] == tuple }
}
func contains<T, U, V, W, X>(_ tuple: (T, U, V, W, X)) -> Bool where T: Equatable, U: Equatable, V: Equatable, W: Equatable, X: Equatable, Element == (T, U, V, W, X) {
contains { [=11=] == tuple }
}
func contains<T, U, V, W, X, Y>(_ tuple: (T, U, V, W, X, Y)) -> Bool where T: Equatable, U: Equatable, V: Equatable, W: Equatable, X: Equatable, Y: Equatable, Element == (T, U, V, W, X, Y) {
contains { [=11=] == tuple }
}
}
if tuples.contains(tuple1) {
print(true)
} else {
print(false) // false
}
if tuples.contains(tuple2) {
print(true) // true
} else {
print(false)
}
虽然元组不是 Equatable
,但您无需编写自己的 contains
版本,因为有一个 contains
版本需要匹配谓词:
if contains(a, { [=10=].0 == 1 && [=10=].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) { [=11=] == (1,2) } // returns true
能够为元组编写 contains
的版本是 nice,但是唉,我认为占位符语法不支持它:
编辑:从 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) { [=12=].0 == x.0 && [=12=].1 == x.1 }
}
let a = [(1,1), (1,2)]
if contains(a, (1,2)) {
println("Yes")
}
这个问题可能太老了希望有人能得到更多选择的帮助。
您可以使用 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")
}
}
Swift 4
将您的代码更改为:
var a = [(1, 1)]
if a.contains(where: { [=10=] == (1, 2) } ) {
print("Yes")
}