我正在尝试将一个 int 值存储在一个元组数组中。 该数组具有以下格式:
var array: [(object1:Object1,object2:Object2,number:Int)]
我有一个表格 View ,其单元格内有按钮和文本字段。 在一个按钮中,我使用操作表字符串选择器为 textFields 赋值。
func buttonPressed(){
let cell = button.superview?.superview? as? CustomCellClass
//i use a string picker to fill the JVFLoatLabeledTextField
// then i acces link the arrays, index to the current cell
let currentIndex = tableView.indexPath(for: cell)
array[currentIndex.row].number = Int(cell.textfield.text!)!
//i also insert the objects in the array, but they work fine
}
但是当我尝试访问来自不同单元格的所有存储数据时 像这样:
for tuple in array {
print(tuple.object1.name)
print(tuple.object2.name)
print(tuple.number)
}
输出将是:object1.name, object2.name, 0 .... 每个 tuple.number 的值为 0。
如果有人提出任何建议,我将不胜感激......
最佳答案
为了缩小问题范围,我会尝试:
数组[currentIndex.row].number = currentIndex.row
如果您在遍历数组时获得预期结果,则表明问题不在于元组数组。检查从表达式返回的值
Int(cell.textfield.text!)!
关于arrays - Swift 无法在元组数组中插入 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51591676/