【发布时间】:2016-10-16 07:25:37
【问题描述】:
我正在尝试创建通用的 CollectionView 数据源。 我有两个协议,第一个 - 一些抽象单元,第二个表示符合类可以由某个抽象单元呈现,并且应该只包含引用该单元的关联类型。他们的实现可能如下所示:
protocol EntityPresentingCell {
// entity that should be presented in this cell
associatedtype T
static var CellReuseID: String { get }
// takes object and fill UI with data
func populate(with object: T)
}
protocol CellPresentable {
// cell that should present this entity
// I need to constrain it
associatedtype Cell: EntityPresentingCell // where Cell.T == Self
}
class CollectionViewDataSource<T: CellPresentable>: NSObject, UICollectionViewDataSource {
var items: [T]
init(items: [T]) {
self.items = items
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: T.Cell.CellReuseID, for: indexPath)
// compiler error here since, obviously, T.Cell.T not constrained to T itself
(cell as! T.Cell).populate(with: items[indexPath.item])
return cell
}
}
在使用时它可能如下所示:
class SomeEntity: CellPresentable {
typealias Cell = SomeEntityCell
var someValue = "someValue"
}
class SomeEntityCell: EntityPresentingCell {
static var CellReuseID: String = "SomeID"
@IBOutlet weak var label: UILabel!
func populate(with object: SomeEntity) {
label.text = object.someValue
}
}
这段代码的问题是我不能限制(因此在编译时确保)CellPresentable.Cell.T 等于 CellPresentable 本身(如示例中所示)。可以清楚地看到编译器错误。
目标是制作纯编译时充分的代码,可以证明给定单元格可以展示该项目(同样,在编译时),我不想强制向下转换或任何其他运行时检查.
有可能吗?如果有,怎么做?
UPD:David Rodrigues answer 有效,但这意味着只有在我即将创建 CollectionViewDataSource 时才会显示不匹配的 (T.Cell.T != T)。我希望它恰好在我定义我的实体符合EntityPresentingCell 协议时发生。换句话说,当我写类似的东西时,编译器应该抱怨
class SomeEntity: CellPresentable {
typealias Cell = SomeWrongEntityCell
var someValue = "someValue"
}
但不是在我创建 CollectionViewDataSource 实例时。确保单元格类型是实体的责任,而不是CollectionViewDataSource 的创建者。
【问题讨论】:
-
@Hamish 这是我问题的答案,如果你把它写成答案,我会接受。
-
我已将评论移至答案 :)
标签: ios swift generics associated-types