Swift - 实现 Hashable 和 Equatable 以及 NSObject 哈希和 isEqual 覆盖之间有什么区别

问题描述

我正在尝试用新的 DiffableDataSource 方式处理 tableviews/collectionviews 中的数据,在测试期间我遇到了一个奇怪的崩溃。 我希望下面的两个实现完全相同:

实施 1:

class DiffableSection {

    var id: String
    var items: [AnyDiffable]

    init(id: String,items: [AnyDiffable] = []) {
        self.id = id
        self.items = items
    }
}

extension DiffableSection: Hashable {
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}

extension DiffableSection: Equatable {
    static func == (lhs: DiffableSection,rhs: DiffableSection) -> Bool {
        return lhs.id == rhs.id
    }
}

实现 2:

class DiffableSection: NSObject {

    var id: String
    var items: [AnyDiffable]

    init(id: String,items: [AnyDiffable] = []) {
        self.id = id
        self.items = items
    }

    // MARK: - Hashable and Equatable

    public override var hash: Int {
        var hasher = Hasher()
        hasher.combine(id)
        return hasher.finalize()
    }

    public override func isEqual(_ object: Any?) -> Bool {
        guard let object = object as? DiffableSection else { return false }
        return id == object.id
    }
}

但显然它们不是 - Implementation 2 可以使用下面的代码,而 Implementation 1 不能。

    func apply(_ sections: [DiffableSection]) {
        var snapshot = self.snapshot()
        for section in sections {
            snapshot.appendSectionIfNeeded(section)
            snapshot.appendItems(section.items,toSection: section)
        }
        apply(snapshot)
    }

(...)
extension NSDiffableDataSourceSnapshot {
    mutating func appendSectionIfNeeded(_ identifier: SectionIdentifierType) {
        if sectionIdentifiers.contains(identifier) { return }
        appendSections([identifier])
    }
}

使用 Implementation 1 运行时收到的崩溃消息:

'NSInternalInconsistencyException',reason: 'Invalid parameter not satisfying: section != NSNotFound'

有人能解释一下这些实现有什么区别吗?我怎样才能修复 Implementation 1 以与 Implementation 2 一样工作??

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)