考虑一个例子:
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"
}
}
但是,由于错误,我不能将它们放在一个数组中:“协议'SomeProtocol'只能用作通用约束,因为它具有Self或相关类型要求”。
有没有办法实现这两个目标:
UICollectionViewCell
的CellConfigurator
参数类型的函数?您可以在CellConfigurator中使用associatedtype:
protocol CellConfigurator: class {
associatedtype CellType where CellType: UICollectionViewCell
func configure(cell: CellType)
}
class AppleCell: UICollectionViewCell {
let title = UILabel()
}
class AppleCellConfigurator: CellConfigurator {
typealias CellType = AppleCell
func configure(cell: CellType) {
cell.title.text = "AAPL"
}
}