我遇到了一个奇怪的情况,我将转换为Set
的领域列表与另一个Set
进行比较,它们具有相同的领域对象,而后者覆盖isEqual
函数。
使用Set
将返回false
!但是,当我将它们转换为Array
时,它会返回true
。
以下是我使用Set
和Array
:
func equal(lhs: Itemization, rhs: Itemization) -> Bool {
let discountChanged = lhs.discounts.count == rhs.discounts.count && Set(lhs.discounts) == Set(rhs.discounts)
return discountChanged // false
}
func equal(lhs: Itemization, rhs: Itemization) -> Bool {
let discountChanged = lhs.discounts.count == rhs.discounts.count && Array(lhs.discounts) == Array(rhs.discounts)
return discountChanged // true
}
我的领域课程:
final class Itemization: Object, Mappable {
.....
let discounts = List<AppliedDiscount>()
override func isEqual(_ object: Any?) -> Bool {
if self === (object as? Itemization) { return true }
if object == nil { return false }
if let object = object as? Itemization {
return equal(lhs: self, rhs: object)
} else {
return false
}
}
}
final class AppliedDiscount: Object {
dynamic var id: String = ""
override func isEqual(_ object: Any?) -> Bool {
if self === (object as? AppliedDiscount) { return true }
if object == nil { return false }
if let object = object as? AppliedDiscount {
return self.id == object.id
} else {
return false
}
}
}
我只想弄清楚发生了什么。