对 Xcode 中成员 'subscript' 的不明确引用 8

Ambiguous Reference to member 'subscript' in Xcode 8

我搜索了关于成员 'subscript' 的模糊引用,但找不到任何解决方案。我正在使用表格视图。这是我使用的代码:-

let  people = [
          ["Pankaj Negi" , "Rishikesh"],
          ["Neeraj Amoli" , "Dehradun"],
          ["Ajay" , "Delhi"]
];
// return the number of section
func numberOfSections(in tableView: UITableView) -> Int {
    return 1;
}

// return how many row
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return people.count;
}

// what are the content of the cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell();

    var (personName , personLocation) = people[indexPath.row] // Ambiguous Reference to member 'subscript'
    cell.textLabel?.text = personName;

    return cell;

}

我是 IOS 开发的新手,为什么我很难理解这一点。但是此代码在 Xcode 6 中有效,但在 Xcode 8 中无效。为什么我不知道?

不要以为相同的代码对你有用 Xcode 6,你在 Xcode 6 中所做的是你制作了元组数组,但目前你正在制作二维数组意味着每个数组元素它自己有两个 String 类型元素的数组。

所以将数组的声明更改为元组数组将消除该错误。

let  people = [
      ("Pankaj Negi" , "Rishikesh"),
      ("Neeraj Amoli" , "Dehradun"),
      ("Ajay" , "Delhi")
]

现在您将在 `cellForRowAt`` 中访问元组

let (personName , personLocation) = people[indexPath.row] 
cell.textLabel?.text = personName

注意:使用Swift不需要添加;来指定语句结束它是可选的,除非你想在单行中添加连续语句