【问题标题】:Ambiguous Reference to member 'subscript' in Xcode 8Xcode 8 中对成员“下标”的模糊引用
【发布时间】:2017-05-13 06:04:14
【问题描述】:

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

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 中无效。为什么我不知道?

【问题讨论】:

    标签: ios swift3 xcode8


    【解决方案1】:

    不要认为相同的代码适用于 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 无需添加 ; 来指定语句结束,除非您想在单行中添加连续语句,否则它是可选的

    【讨论】:

    • 谢谢,@Nirav D,我制作了二维数组,但我需要一个元组数组,在这种情况下,这就是它显示此错误的原因。使用 '[' 代替 '(' 是我的愚蠢错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多