I have a class with the protocol Equatable
. The class looks like this:
class Item: Equatable {
let item: [[Modifications: String]]
init(it
In your ==
function for Item
objects, you need to specify further how to compare two types of arrays of dictionaries (specifically, two types of [[Modifications: String]]
).
The following working solution compares your item
arrays element by element (dictionary by dictionary), and ==
returns true only if the arrays contain the same number of dictionaries, and if all entries are alike and ordered the same fashion in the array of dictionares
func ==(lhs: Item, rhs: Item) -> Bool {
if lhs.item.count == rhs.item.count {
for (i, lhsDict) in lhs.item.enumerate() {
if lhsDict != rhs.item[i] {
return false
}
}
return true
}
else {
return false
}
}
class Item : Equatable {
let item: [[Modifications: String]]
init(item: [[Modifications: String]]) {
self.item = item
}
}
You probably want to modify this into the form you actually want to use for comparison, but I hope you get the gist of it.
Note also that, if testing this in a playground, it's important that your ==
function definition func ==(lhs: Item, rhs: Item) -> Bool { ..
should precede your class definition, otherwise you will get an error of nonconformance to Equatable
.