对 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 中所做的是您制作了元组数组,但目前您正在制作 2D 数组意味着每个数组元素它本身都有带有两个字符串类型元素的数组。

因此,将数组的声明更改为元组数组将删除该错误。

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

现在,您将访问"cellForRowAt"中的元组

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

注意:使用 Swift 无需添加;来指定语句结尾,否则它是可选的,除非您想在单行中添加连续语句

最新更新