ios - 更新具有特定索引的元组数组

标签 ios arrays swift tuples

我无法尝试更新我的多维数组。首先,我过滤了一个名为 title 的特定字符串,它用作我的标识符。然后我想替换这个过滤后的数组的描述值。

我已经尝试用类似 rowContent[1] = descriptionTranferred 的方式解决这个问题(索引 1 处的值是描述,应替换为 descriptionTranferred 中的值)。我也尝试了 rowContent.description = descriptionTranferred,但也没有成功。

有什么方法可以更新数组中的描述值吗?

这是我的代码:

SWIFT 3

var rowContent = Array<(title: String, description: String)>()


func transferData(descriptionTranferred: String, identifier: String) {
    if rowContent.contains(where: {$0.title == identifier}) {

        rowContent[1] = descriptionTranferred

    } else {
        print ("nothing")
    }
}

最佳答案

它不是多维数组,而是具有命名参数的元组数组。尝试:

rowContent[1].description = descriptionTranferred

但我猜您正在尝试更改元组中与标识符匹配的索引的描述,因此请替换您的 if 条件:

if let index = rowContent.index(where: {$0.title == identifier}) {
    rowContent[index].description = descriptionTranferred
}else {
    print ("nothing")
}

关于ios - 更新具有特定索引的元组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40007386/

相关文章:

iphone - 为什么我可以使用locationInView,但不能使用translationInVew

c++ - 程序可以掷 2 个骰子 36000 次

c++ - 如何将文件中的长字符串读入数组或类似的东西?

c++ - CUDA cudaMemcpy 数组结构

ios - 尝试在 iPad 上显示后置摄像头时应用程序卡住

ios - 如何禁用 subview 后面的用户交互?

ios - 更改xi​​b名称

ios - Xcode 7.1 中没有 "iOS Device"选项

ios - AppDelegate 中需要任何用于第二个核心数据实体的代码吗?

ios - 如何删除使用 swift 代码添加的 3D touch 快速操作