我正在尝试编写一个CollectionDataAdapter,如下所示:
public protocol InitializableCell {
associatedtype T
init(item: T)
}
public typealias ACCollectionNodeCellAction = (_ collectionNode: ASCollectionNode, _ indexPath: IndexPath, _ object: Any) -> Void
open class ASCollectionNodeAdapter<Provider: CollectionDataProvider, Cell: ASCellNode>:
NSObject,
ASCollectionDataSource,
ASCollectionDelegate
where Cell: InitializableCell, Provider.T == Cell.T
{
// MARK: - Delegates
public var didSelectCell: ACCollectionNodeCellAction?
// MARK: - Private Properties
let provider: Provider
let collectionNode: ASCollectionNode
// MARK: - Lifecycle
init(collectionNode: ASCollectionNode, provider: Provider) {
self.collectionNode = collectionNode
self.provider = provider
super.init()
setUp()
}
func setUp() {
collectionNode.dataSource = self
collectionNode.delegate = self
}
// MARK: - ASCollectionDataSource
public func numberOfSections(in collectionNode: ASCollectionNode) -> Int {
return provider.numberOfSections()
}
public func collectionNode(_ collectionNode: ASCollectionNode, numberOfItemsInSection section: Int) -> Int {
return provider.numberOfItems(in: section)
}
public func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForItemAt indexPath: IndexPath) -> ASCellNodeBlock {
return { [weak self] () -> ASCellNode in
guard let `self` = self, let item = self.provider.item(at: indexPath) else {
return ASCellNode()
}
return Cell.init(item: item)
}
}
// MARK: - ASCollectionDelegate
}
问题是我无法初始化类型ASCellNode
的Cell并且符合InitializableCell
说:
参数标签'(item :)'与任何可用的重载都不匹配
如果我这样调用init:Cell(item: item)
编译器抱怨:
非标称类型'Cell'不支持显式初始化
怎么回事?因为Cell已经符合InitializableCell
协议