gpt4 book ai didi

swift - 类型 'Int' 没有下标成员?

转载 作者:搜寻专家 更新时间:2023-11-01 06:57:17 25 4
gpt4 key购买 nike

我正在用 Swift 编写我的第一个应用程序。如果这是重复的,我很抱歉,因为我查看了其他错误并且不确定如何将他们获得的帮助插入我的代码中。

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return textView.text.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let myText = Int(entertextTextView.text!)
cell.textLabel?.text = "\(myText?[myText.index(myText.startIndex, offsetBy: indexPath.row)])"
return cell
}

我收到“Type 'Int' 没有下标成员”错误。我正在尝试为 TextView 中的每个字符将一个新单元格添加到 TextView 中。

最佳答案

如果要向每个单元格添加一个字符,则不需要将其转换为 Int。只有当它是一个字符串时,您才能使用索引访问字符。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let myText = entertextTextView.text!
cell.textLabel?.text = "\(myText[myText.index(myText.startIndex, offsetBy: indexPath.row)])"
return cell
}

此外,您的内容是否为数字并不重要,这就是为什么我假设您将其更改为 Int。除非你对数字进行算术运算或类似的操作,否则你可以让内容为 String

关于swift - 类型 'Int' 没有下标成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52826991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com