2017-05-13 38 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數組,但我需要一個元組的數組,在這種情況下,這就是它顯示這個錯誤的原因。用'['代替'(')是我這邊愚蠢的錯誤。 –

相關問題