ios - 无法将“Int”类型的值转换为预期的参数类型“String.Index”
问题描述
我收到此错误:
无法将“Int”类型的值转换为预期的参数类型“String.Index”
上线Goals[0].remove(at: indexPath.row)
。我该如何解决这个问题?
这是我的代码:
import UIKit
class GoalsViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var Goals: [String] = ["goal 1", "goal 2", "goal 3"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
}
extension GoalsViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
Goals[0].remove(at: indexPath.row)
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Goals.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "GoalCell_1", for: indexPath)
cell.textLabel?.text = Goals[indexPath.row]
return cell
}
}
解决方案
目标是一个数组。
var Goals: [String] = ["goal 1", "goal 2", "goal 3"]
在这里你选择数组'[0]'的第一个元素并尝试从中'.remove',但它是一个字符串
Goals[0].remove(at: indexPath.row)
如果要从目标数组中删除字符串:
Goals.remove(at: indexPath.row)
推荐阅读
- c# - 如何在 C# 中更改输入框的属性
- android-studio - 在平板电脑上运行时背景图像不占据整个屏幕
- php - Vue.js 请求发送令牌,在真实服务器上测试时突然失败并出现 401
- r - 使用 [ 按名称删除列而不为数据框/矩阵对象分配名称
- intellij-idea - IntelliJ IDEA 未在 Nuxt.js 项目中检测 Vuetify 组件
- angular - 使用 firebase 部署时在 Angular 6 应用程序中找不到应用程序根选择器
- java - 从可选列表中流式传输
- c# - 在不加载导航属性的情况下更新关系
- c# - 即使两个列表中都不存在项目,如何获取两个列表中的所有项目?
- node.js - 无法在 ubuntu 16.04 的 80 端口运行我的节点应用程序