问题描述
这是在tableview cellforrowatindexpath里面
This is inside tableview cellforrowatindexpath
var valueArray:[(String,String)] = []
if !contains(valueArray, v: (title,status)) {
let v = (title,status)
valueArray.append(v)
}
这是在didselectrowatIndexPath里面
This is inside didselectrowatIndexPath
let cell = self.tableView.cellForRowAtIndexPath(selectedRow!)
var newTuple = (cell!.textLabel!.text!, cell!.detailTextLabel!.text!)
let index = valueArray.indexOf(newTuple)
但我没有得到索引。抛出错误无法将类型'(String,String)'的值转换为预期的参数类型'@noescape((String,String))throws - > Bool'。我在这里做错了什么?
But i am not getting the index. It is throwing an error cannot convert value of type '(String,String)' to expected argument type '@noescape ((String,String)) throws -> Bool'. What i am doing wrong here?
推荐答案
可以比较元组的相等性(从Swift 2.2 / Xcode 7.3.1开始),但
他们不符合 Equatable
协议。因此,您需要
来使用基于谓词的 indexOf
变体来定位数组中的元组
。示例:
Tuples can be compared for equality (as of Swift 2.2/Xcode 7.3.1), butthey do not conform to the Equatable
protocol. Therefore you haveto use the predicate-based variant of indexOf
to locate a tuplein 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)
}
这篇关于如何从元组数组中找到元组元素的索引? iOS,Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!