[ad_1]
Beforehand, when assortment view solely show 1 merchandise sort, I’ve no challenge in making use of CollectionDifference<T>.
My code snippet appears to be like as the next.
struct Guidelines: Hashable {
...
}
non-public var uncheckedChecklists: [Checklist] = []
non-public typealias DataSource = UICollectionViewDiffableDataSource<SectionIdentifier, Guidelines>
non-public typealias Transaction = NSDiffableDataSourceTransaction<SectionIdentifier, Guidelines>
non-public func makeDataSource() -> DataSource {
...
dataSource.reorderingHandlers.didReorder = { [weak self] transaction in
guard let self = self else { return }
self.didReorder(transaction)
}
}
non-public func didReorder(_ transaction: Transaction) {
guard let uncheckedChecklists = uncheckedChecklists.making use of(transaction.distinction) else { return }
}
Just lately, I must introduce further merchandise sort to assortment view. Therefore, my assortment view now to assist 2 merchandise sort. I refactor my code to as comply with
struct Guidelines: Hashable {
...
}
// Further merchandise sort.
struct Recording: Hashable {
...
}
non-public var uncheckedChecklists: [Checklist] = []
non-public typealias DataSource = UICollectionViewDiffableDataSource<SectionIdentifier, AnyHashable>
non-public typealias Transaction = NSDiffableDataSourceTransaction<SectionIdentifier, AnyHashable>
non-public func makeDataSource() -> DataSource {
...
dataSource.reorderingHandlers.didReorder = { [weak self] transaction in
guard let self = self else { return }
self.didReorder(transaction)
}
}
non-public func didReorder(_ transaction: Transaction) {
// Forged from 'CollectionDifference<AnyHashable>' to unrelated sort 'CollectionDifference<Guidelines>' at all times fails
guard let distinction = transaction.distinction as? CollectionDifference<Guidelines> else { return }
guard let uncheckedChecklists = uncheckedChecklists.making use of(distinction) else { return }
}
I’m having challenge in carry out correct generic casting in didReorder perform.
For the reason that we solely permit reordering for merchandise sort Guidelines (through dataSource.reorderingHandlers.canReorderItem perform), throughout didReorder, it’s fairly certain the AnyHashable will likely be sort Guidelines.
However, I’m not certain, how can we carry out correct casting, to solid CollectionDifference<AnyHashable> to CollectionDifference<Guidelines>. Do you’ve got any suggestion? Thanks.
[ad_2]
