'subscript(_ :)'不可用:不能对带有Int的字符串进行下标(Swift 5)

问题描述 投票:0回答:1

我在这里只找到一个答案。我的问题是我不使用Character,除了String是-字符集合。我尝试将其设置为String

        let name = String(array[indexPath.section][indexPath.row])

但是出现相同的错误无法将类型为'Character'的值分配为类型为'String?'

这是我的手机:

struct SectionStrings {
    let sections = ["Stuff", "More Stuff", "Best Stuff"]
}

struct ArrayStrings {
    let array = ["Little Stuff", "Bigger Stuff", "Biggest Stuff"]

}

struct DetailArrayStrings {
    let detailArray = ["Teeny Stuff", "Teentsie Weentsie Stuff", "Invisible Stuff"]

}

这里是我的控制器:

let sections = SectionStrings().sections
var array = ArrayStrings().array
let detailArray = DetailArrayStrings().detailArray

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! SettingsCell

        let name = array[indexPath.section][indexPath.row] // errors here
        cell.textLabel?.text = name
        cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        cell.textLabel?.textColor = .white

        let detailName = detailArray[indexPath.section][indexPath.row] // and here
        cell.detailTextLabel?.text = detailName
        cell.detailTextLabel?.font = UIFont.systemFont(ofSize: 15)
        cell.detailTextLabel?.textColor = .white
        cell.detailTextLabel?.numberOfLines = 0

        cell.selectionStyle = .none
        cell.backgroundColor = .black

        return cell
    }
swift uitableview character subscript custom-sections
1个回答
0
投票

您仅应使用name来获得indexPath所需的row

let name = array[indexPath.row]
cell.textLabel?.text = name
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 17)
cell.textLabel?.textColor = .white

let detailName = detailArray[indexPath.row]
© www.soinside.com 2019 - 2024. All rights reserved.