Update: SE-0143 Conditional conformances has been implemented in Swift 4.2.
As a consequence, your code does compile now. And if you define Item
as a struct
struct Item: Equatable {
let item: [[Modifications: String]]
init(item: [[Modifications: String]]) {
self.item = item
}
}
then the compiler synthesizes the ==
operator automatically,
compare SE-0185 Synthesizing Equatable and Hashable conformance
(Pre Swift 4.1 answer:)
The problem is that even if ==
is defined for the dictionary type
[Modifications: String]
, that type does not conform to
Equatable
. Therefore the array comparison operator
public func ==<Element : Equatable>(lhs: [Element], rhs: [Element]) -> Bool
cannot be applied to [[Modifications: String]]
.
A possible concise implementation of ==
for Item
would be
func ==(lhs: Item, rhs: Item) -> Bool {
return lhs.item.count == rhs.item.count
&& !zip(lhs.item, rhs.item).contains {$0 != $1 }
}
Your code compiles for [[String: String]]
– if the Foundation
framework is imported, as @user3441734 correctly said – because then [String: String]
is automatically converted to NSDictionary
which conforms to
Equatable
. Here is a "proof" for that claim:
func foo<T : Equatable>(obj :[T]) {
print(obj.dynamicType)
}
// This does not compile:
foo( [[Modifications: String]]() )
// This compiles, and the output is "Array<NSDictionary>":
foo( [[String: String]]() )
==
function inside the class definition in order for it to conform to theEquatable
protocol. – Ariew[[String: String]]
there's no problem, so 2d dicts should be able to be compared. @milo526: Because the enum has a raw type value, it becomes equatable automatically, if im correct. @Leo Dabus: yes, I'm trying to think of another way but it would be much easier if its an array of dictionaries. – Bathe[Modifications: String]
with==
, but[Modifications: String]
does not conform toEquatable
. Therefore==
is not defined for an array of those dictionaries. – My guess is that is works for[[String: String]]
because[String: String]
can be bridged to NSDictionary (which is Equatable again). – Kittenish[Modifications: String]
. I guess I can still use[[String: String]
but using 'Modifications.description` as key. Still I hope it's possible to fix this... – Bathe