Hi guys.
I am trying to implement collection view screen with multiple sections and cell types using diffable data source. I define it using Sections enum and SectionItem struct which keeps nested model for cells configuration
private var dataSource: UICollectionViewDiffableDataSource<Behavior.Section, SectionItem>?
public typealias HashableAndSendable = Hashable & Sendable
public protocol ItemProtocol: HashableAndSendable {
var model: any HashableAndSendable { get }
}
public struct SectionItem: ItemProtocol {
public var model: any HashableAndSendable
public func hash(into hasher: inout Hasher) {
hasher.combine(model)
}
public static func == (lhs: SectionItem, rhs: SectionItem) -> Bool {
lhs.model.hashValue == rhs.model.hashValue
}
}
I want to have cells with configure function with its own model struct as a param
public protocol ConfigurableProtocol {
associatedtype Model: HashableAndSendable
@MainActor
func configure(with model: Model)
}
public typealias ConfigurableCell = UICollectionViewCell & ConfigurableProtocol
In my View Controller I am setting up my cells using UICollectionView.CellRegistration handler
var cellsRegistrations = [String: Any]()
func registerCell<Cell>(
cellType: Cell.Type,
handler: @escaping UICollectionView.CellRegistration<Cell, SectionItem>.Handler
)
where Cell: ConfigurableCell {
cellsRegistrations[String(describing: cell.self)] = UICollectionView.CellRegistration<Cell, SectionItem>(handler: handler)
}
func setupCells() {
for type in behavior.cellsTypes() {
registerCell(cellType: type) { [unowned self] cell, indexPath, itemIdentifier in
configure(cell: cell, model: itemIdentifier.model)
}
}
}
but when trying to call configure functions I have no way of casting to proper model type of a cell
func configure(
cell: some ConfigurableCell,
model: some HashableAndSendable
) {
// error here: 'some' types are only permitted in properties, subscripts, and functions
cell.configure(with: model as? (some ConfigurableCell).Model)
}
Can I define these relationships like that or not ? What can you recommend
Thanks a lot