0

I'm trying to wrap my head around the new DiffableDataSource way of handling data in tableviews/collectionviews and during testing I came across a strange crash. I would expect that the two implementations below would work exactly the same:

Implementation 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
    }
}

Implementation 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
    }
}

but apparently they are not - Implementation 2 works with the code below and Implementation 1 does not.

    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])
    }
}

The crash message I get when running with Implementation 1:

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

Can someone explain me what are the differences in those implementations? How could I fix Implementation 1 to work same as Implementation 2??

L3M0L
  • 429
  • 8
  • 23
  • 1
    Does this answer your question? [NSObject subclass in Swift: hash vs hashValue, isEqual vs ==](https://stackoverflow.com/q/33319959/1187415). – Martin R May 17 '21 at 19:43
  • @MartinR The link you provided shows me that I indeed need to override `isEqual` to make it work however after reading the highest rated comment I still don't understand what's the difference between `==` :/ – L3M0L May 17 '21 at 20:01

0 Answers0