UICollectionViewDiffableDataSource - 具有不同散列的相同项目时崩溃

问题描述 投票:0回答:1

我正在使用

UICollectionViewDiffableDataSource
来填充
UICollectionView
数据。我的理解是
DiffableDataSource
使用
==
比较项目,然后如果项目相等,它会比较
hash
值以查看是否发生了变化。

但根据我收到的错误,情况并非如此。

Diffable data source detected item identifiers that are equal but have different hash values. Two identifiers which compare as equal must return the same hash value. You must fix this in the Hashable (Swift) or hash property (Objective-C) implementation for the type of these identifiers

就我而言,我有与 uniqueID 进行比较的项目,并且 hashValue 由用户输入的值确定。如果 == 和 hashValue 不能不同,那么使用它们有什么意义?

swift uicollectionview uikit uicollectionviewdiffabledatasource
1个回答
0
投票

解决方法:通过 hashValue 而不是 uniqueID 进行 equaltable 以避免

equal but have different hash values
崩溃错误。

也许不是一个完美的解决方案。

static func == (lhs: Item, rhs: Item) -> Bool {
   lhs.hashValue == rhs.hashValue
}
© www.soinside.com 2019 - 2024. All rights reserved.