更新: SE-0143 条件付き適合がSwift 4.2で実装されました。
結果として、コードは今すぐコンパイルされます。そして、構造体Itemとして定義する場合
struct Item: Equatable {
let item: [[Modifications: String]]
init(item: [[Modifications: String]]) {
self.item = item
}
}
その後、コンパイラは==演算子を自動的に合成します。 SE-0185 Synthesizing Equatable and Hashable 適合性を比較してください
(Swift 4.1以前の回答:)
問題は==、辞書型 に対して が定義されて
いても[Modifications: String]、その型が に準拠していないこと
Equatableです。したがって、配列比較演算子
public func ==<Element : Equatable>(lhs: [Element], rhs: [Element]) -> Bool
には適用できません[[Modifications: String]]。
==forの可能な簡潔な実装は次のようにItemなります。
func ==(lhs: Item, rhs: Item) -> Bool {
return lhs.item.count == rhs.item.count
&& !zip(lhs.item, rhs.item).contains {$0 != $1 }
}
[[String: String]]@ user3441734が正しく言ったように、Foundationフレームワークがインポートされている場合、コードはコンパイルされます。これは、に準拠するものに
[String: String]自動的に変換されるためです。その主張の「証拠」は次のとおりです。NSDictionaryEquatable
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]]() )