iOS Realm列表为Set或Array相等

时间:2017-08-21 07:58:35

标签: ios realm realm-list

我遇到了一个奇怪的情况,我将转换为Set的领域列表与另一个Set进行比较,它们具有相同的领域对象,而后者覆盖isEqual函数。 使用Set将返回false!但是,当我将它们转换为Array时,它会返回true

以下是我使用SetArray

的代码
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
        }
    }
}

我只想弄清楚发生了什么。

0 个答案:

没有答案