更新时间:2023-11-12 21:27:16
可以比较元组是否相等(从 Swift 2.2/Xcode 7.3.1 开始),但是它们不符合 Equatable
协议.因此你有使用 indexOf
的基于谓词的变体来定位元组在一个数组中.示例:
Tuples can be compared for equality (as of Swift 2.2/Xcode 7.3.1), but
they do not conform to the Equatable
protocol. Therefore you have
to use the predicate-based variant of indexOf
to locate a tuple
in an array. Example:
let valueArray = [("a", "b"), ("c", "d")]
let tuple = ("c", "d")
if let index = valueArray.indexOf({ $0 == tuple }) {
print("found at index", index)
}
在 Swift 4 中,该方法已重命名为 firstIndex(where:)
:
In Swift 4 the method has been renamed to firstIndex(where:)
:
if let index = valueArray.firstIndex(where: { $0 == tuple }) {
print("found at index", index)
}