我有以下代码
if let id = items[indexPath.row]["id"] {
if let idx = savedGoals.index(of: id) {
savedGoals.remove(at: idx)
}
}
我得到了错误Type 'Any' cannot conform to 'Equatable'
在线的if let idx = savedGoals.index(of: id) {
savedGoals 声明为
var savedGoals = []
有人知道我为什么会收到这个错误吗?
您将希望更加明确地使用类型,以尽量减少使用
Any
:您说
savedGoals
已声明为:我很惊讶这竟然真的起作用了,因为通常这会导致如下错误:
无论如何,您可以通过指定类型来解决这种歧义,例如:
我们无法
id
从这些代码片段中判断这是什么类型,但希望这可以说明这个想法。假设
items
是字典数组,你可能还需要更具体指定 的类型id
。因此,不要这样:也许: