【发布时间】:2018-07-25 03:41:41
【问题描述】:
考虑一个例子:
protocol CellConfigurator {
var cellClass: UICollectionViewCell.Type {get}
func configure(cell: UICollectionViewCell)
}
class AppleCell: UICollectionViewCell {
let title = UILabel()
}
class AppleCellConfigurator: CellConfigurator {
let cellClass: UICollectionViewCell.Type = AppleCell.self
func configure(cell: UICollectionViewCell) {
guard let cell = cell as? AppleCell else {return}
cell.title.text = "AAPL"
}
}
我可以使用上述模式来封装UICollectionViewCell的实际类型,使用如下(伪代码):
func cellAt(indexPath: IndexPath) -> UICollectionViewCell {
let configurator = configurators[indexPath]
let cell = collectionView.dequeueReusableCell(identifier: String(describing: configurator.cellClass))
configurator.configure(cell)
return cell
}
我期待摆脱对每个符合 CellConfigurator 的单元进行类型转换的必要性,例如,使用具有关联类型的协议:
protocol CellConfigurator {
associatedtype Cell
func configure(cell: Cell)
}
class AppleCell: UICollectionViewCell {
let title = UILabel()
}
class AppleCellConfigurator: CellConfigurator {
typealias Cell = AppleCell
func configure(cell: Cell) {
cell.title.text = "AAPL"
}
}
但是,由于错误,我无法将它们放在一个数组中:“Protocol 'SomeProtocol' 只能用作通用约束,因为它具有 Self 或关联的类型要求”。
有没有办法实现这两个目标:
- 对于任何
CellConfigurator都有一个带有UICollectionViewCell参数类型的函数? - 在特定配置器的函数中具有具体类型
【问题讨论】:
-
我认为您可以创建一个符合
CellConfigurator的通用基类,然后AppleCellConfigurator将从该类继承。 -
@Kamran 我认为这是一个很好的类型擦除案例。