2017-05-13 26 views
0

我有搜索关于歧义引用成员'下标'但找不到任何解决方案。我正在使用TableView。这是我正在使用的代码: -对Xcode 8中成员'下标'的歧义引用

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

回答

0

不要这么认为相同的代码适合你Xcode 6,你在Xcode 6中做了什么是你已经制作了一个元组数组,但是目前你正在制作2D数组意味着每个数组元素都有自己的数组两个String类型元素。

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

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

现在你会在你的`cellForRowAt``访问元组

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

注:与SWIFT无需添加;指定语句的结束是可选的,除非您要添加的连续声明在单行

+0

谢谢,@Nirav D,我做了2D数组,但我需要一个元组的数组,在这种情况下,这就是它显示这个错误的原因。用'['代替'(')是我这边愚蠢的错误。 –

相关问题