I have the following code
if let id = items[indexPath.row]["id"] {
if let idx = savedGoals.index(of: id) {
savedGoals.remove(at: idx)
}
}
I get the error Type 'Any' cannot conform to 'Equatable'
on line if let idx = savedGoals.index(of: id) {
savedGoals is declared as
var savedGoals = []
does anyone know why I’m getting this error?
2
You will want to be more explicit in your use of types, to minimize the use of Any
:
-
You say that
savedGoals
has been declared as:var savedGoals = []
I am surprised that worked at all, as usually that would result in yield an error such as:
Empty collection literal requires an explicit type
Regardless, you can resolve this ambiguity by specifying a type, e.g., either:
var savedGoals: [Int] = [] // or `[String]` or whatever
We can’t tell what type this
id
is from these code snippets, but hopefully this illustrates the idea. -
Assuming that
items
is an array of dictionaries, you will likely need to be more specific in the type ofid
, too. Thus, instead of:if let id = items[indexPath.row]["id"] {…}
Perhaps:
if let id = items[indexPath.row]["id"] as? Int {…} // or `as? String` or whatever