类型"Int"没有下标成员?



我正在用 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
}

我收到"类型'Int'没有下标成员"错误。我正在尝试为文本视图中的每个字符在文本视图中添加一个新单元格。

如果要向每个单元格添加一个字符,则不需要将其转换为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

最新更新