尝试从数组中删除元素时出现此错误:上下文类型“Int”不能与数组文字一起使用
Posted
技术标签:
【中文标题】尝试从数组中删除元素时出现此错误:上下文类型“Int”不能与数组文字一起使用【英文标题】:Trying to remove an element from an array i get this error: Contextual type 'Int' cannot be used with array literal 【发布时间】:2018-03-01 12:13:58 【问题描述】:我在 tableView 中为 reloadCell 添加了一个按钮,我想在按下按钮时删除特定单元格上的数组元素,但我收到此错误
上下文类型 'Int' 不能与数组字面量一起使用
@IBAction func reloadCell(_ sender: UIButton)
let index = IndexPath(row: sender.tag, section: 0)
sortedArray.remove(at: [index]) //HERE I GET THE ERROR
self.tableView.reloadRows(at: [index], with: .right)
我该如何解决?
【问题讨论】:
sortedArray.remove(at: sender.tag) 你需要整数而不是 IndexPath 类 【参考方案1】:数组使用 Int
s 作为索引,而不是像 tableView
那样使用 IndexPath
s:
sortedArray.remove(at: sender.tag)
【讨论】:
【参考方案2】:你不能对数组使用 IndexPath 类型(你应该使用 int 类型) 例如:
sortedArray.remove(at: index.row)
self.tableView.reloadRows(at: index.row, with: .right)
【讨论】:
以上是关于尝试从数组中删除元素时出现此错误:上下文类型“Int”不能与数组文字一起使用的主要内容,如果未能解决你的问题,请参考以下文章