【发布时间】:2019-06-08 18:30:47
【问题描述】:
我正在开发一个 iOS 应用,我想使用 Array() 访问字典中的特定值。
我的字典包含一个数组,其中包含结构。
let array = [(key: "S", value: [Thunderbolt.repoStruct(repoName: "Semiak Repo", repoURL: "https://repo.semiak.dev", icon: Optional("iconRound"))]), (key: "T", value: [Thunderbolt.repoStruct(repoName: "Thunderbolt iOS Utilities", repoURL: "https://repo.thunderbolt.semiak.dev", icon: Optional("iconRound"))])]
我正在用数组创建一个UITableView:部分名称是key 值,单元格标题是repoStruct.repoName 值,以下值相同。
要访问 repoName,我会使用 Array(array)[0].1[0].repoName。
问题是我不知道我想访问的确切位置,而是使用 indexPath 来知道我需要哪个值:
Array(array)[indexPath.section].indexPath.row[0].repoName
这应该返回单元格的 repoName,但是却给了我以下错误:Value of tuple type '(key: String, value: [repoStruct])' has no member 'indexPath'
我也尝试过使用:
let row = indexPath.row
Array(array)[indexPath.section].row[0].repoName
但它给了我同样的错误:Value of tuple type '(key: String, value: [repoStruct])' has no member 'row'
我不知道为什么 Array(array)[0].1 有效并返回值,但 Array(array)[indexPath.section].row 没有。它也是这样做的:使用位置访问一个值,它是一个 int,例如 indexPath。
我怎样才能做到这一点?
提前致谢。
【问题讨论】:
标签: arrays swift dictionary tuples nsdictionary