我注意到,当使用UITableViewDiffableDataSource / UICollectionViewDiffableDataSource时,ItemIdentifierType
使用时
var managedDataSource: UITableViewDiffableDataSource<String, StringCellObject>!
class StringCellObject: Hashable {
let value: String
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
static func == (lhs: StringCellObject, rhs: StringCellObject) -> Bool {
return lhs.value == rhs.value
}
init(value: String) {
self.value = value
}
}当StringCellObject是类时,甚至不调用可访问的函数,并且对象总是被视为更改(即使值是相同的)。
当我将类更改为结构时
struct StringCellObject: Hashable {我得到正确的行为
为什么在使用类时行为发生了变化,以及如何获得预期的行为?
发布于 2020-11-30 03:49:02
我认为您必须实现如下所示的可执行的方法。
class Item: Hashable {
static func == (lhs: Item, rhs: Item) -> Bool {
lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
let id = UUID()
}https://stackoverflow.com/questions/61754786
复制相似问题