简体   繁体   中英

How can I cast a an array of protocol X to `Hashable` if the class conform to X is Hashable?

I have a protocol my TableView cell controllers conform too:

protocol TableViewCellController {
  func view(in tableView: UITableView) -> UITableViewCell
  func cancelLoad()
  func prefetch()
}

extension TableViewCellController {
  func prefetch() { }
}

As my table view renders multiple cell types, each type of item as its own controller


final class ContentArticleTitleCellController: TableViewCellController {

  private var cell: ContentArticleTitle?

  func view(in tableView: UITableView) -> UITableViewCell {
    cell = tableView.dequeueReusableCell()
    return cell!
  }
.....
}

final class ContentArticleSummaryCellController: TableViewCellController {

  private var cell: ContentArticleSummary?

  func view(in tableView: UITableView) -> UITableViewCell {
    cell = tableView.dequeueReusableCell()
    return cell!
  }
.....
}

As all of these controllers conform to Hashable , I'd like to use UITableViewDiffableDataSource .

I can create my data source using AnyHashable like so

  func makeDataSource() -> UITableViewDiffableDataSource<Section, AnyHashable> {
    return UITableViewDiffableDataSource(tableView: tableView) { tv, indexPath, controller -> UITableViewCell? in
      guard let controller = controller as? TableViewCellController else { return nil }
      return controller.view(in: tv)
    }
  }

I am unsure how to cast my [TableViewCellController] to [AnyHashable] however as TableViewCellController does not conform to it, rather the class that is conforming to it.

I had considered

  func update(with items: [TableViewCellController], animate: Bool = true) {
    let controllers = items.compactMap { $0 as? AnyHashable }
  }

But as these types are unrelated it always fails.

If I try

let controllers = items.compactMap { $0 as? Hashable }

I then hit the generic constraint because it has Self or associated type requirements

Of course I can do something like

let controllers = items.compactMap { $0 as? ContentArticleSummaryCellController }

But if I have 30 different cell controller types this becomes messy

How can I achieve this?

You were close with this approach:

let controllers = items.compactMap { $0 as? AnyHashable }

Change it to:

let controllers = items.map(AnyHashable.init)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM