Skip to content

Introduce AnyCellConfigurable protocol #1

@mbaranowski

Description

@mbaranowski

@luketomlinson here is an idea from my own experiment along these lines:

You could introduce a new protocol that CellConfigurable inherits from like this:

protocol AnyCellConfigurable {
   func configure(with viewModel: CellViewModelType) // can also be just Any
}
protocol CellConfigurable: AnyCellConfigurable {
    associatedtype ViewModelType: CellViewModelType
    func configure(with viewModel: ViewModelType)
}
extension CellConfigurable {
    func configure(with viewModel: CellViewModelType) {
        // this will crash only in the same case that InstrumentViewController. configureCell reaches the default case with the fatalError
        configure(with: viewModel as! ViewModelType) 
}

Assuming that your tableView is configured to dequeue only cells of type AnyCellConfigurable, you can then replace configureCell(_ cell: UITableViewCell, cellViewModel: CellViewModelType) with just a call like cell.configure(with: viewModel) and they should be functionally equivalent with less boiler plate in the second case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions