【发布时间】:2015-11-24 06:07:45
【问题描述】:
我想要的是为某些符合某些协议的 NSObjectProtocol 自动实现委托方法,但我很努力,没有完成。
演示在下方
更新更准确
================================================ ============================
我有一个协议PagedLoadable 来获取collectionView 需要什么的信息,然后是extension NSObjectProtocol where Self: Delegatable,对象的自动配置实现PagedLoadable
protocol PagedLoadable {
var count: Int { get }
}
protocol Delegatable: UICollectionViewDelegate, UICollectionViewDataSource {
}
extension PagedLoadable where Self: Delegatable {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return count
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = UICollectionViewCell()
return cell
}
}
class vc: UIViewController {
}
extension vc: PagedLoadable {
var count: Int {
return 1
}
}
extension vc: Delegatable {
}
【问题讨论】:
-
您遇到的错误是什么?
-
类型'vc'不符合协议'UICollectionViewDataSource'
-
为什么不直接定义
Delegatable的扩展? -
@findall 你能告诉我吗? T_T
-
@InvokerLaw 对不起,我已经测试了我所说的,但是编译器由于段错误而崩溃,即使它通过了。 (我试过
extension Delegatable { /* Implementations required by UICollectionViewDataSource */ })
标签: swift swift-protocols