Perché l'ultima istruzione non riescono a compilare con l'errore: Binary operator '==' cannot be applied to two '[[Simple]]’ operands
, e c'è un modo modo per modificare la Simple
struct o estendere l'operatore ==
di essere in grado di eseguire i controlli di uguaglianza su matrici nidificate (o dizionari)?Swift operatore di uguaglianza su matrici nidificate
var i1: [Int] = [1]
var i2: [Int] = [1]
i1 == i2 // -> true
var i3: [[Int]] = [[1], [2]]
var i4: [[Int]] = [[1], [2]]
i3 == i4 // -> true
struct Simple: Equatable, Hashable {
let message: String
var hashValue: Int {
return message.hashValue
}
}
func ==(lhs: Simple, rhs: Simple) -> Bool {
return lhs.message == rhs.message
}
var a: [Simple] = [Simple(message: "a")]
var b: [Simple] = [Simple(message: "a")]
a == b // -> true
var x: [[Simple]] = [[Simple(message: "a")], [Simple(message: "b")]]
var y: [[Simple]] = [[Simple(message: "a")], [Simple(message: "b")]]
x == y // -> ERROR! Binary operator '==' cannot be applied to two '[[Simple]]’ operands
Forse aggiornare l'implementazione a 'lhs.elementsEqual (rhs, di: ==)'? – kennytm
@kennytm: buon suggerimento, grazie! –
Suggerimento: poiché https://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md non è stato incluso in Swift 4, aggiornare la risposta –