Swift2协议扩展NSObjectProtocol
我想要的是为某些NSObjectProtocol自动实现委托方法,它符合某些协议,但我努力了,没有完成 下面是演示 更新以获得更准确的信息 ========================================================================= 我得到了一个协议Swift2协议扩展NSObjectProtocol,swift,swift-protocols,Swift,Swift Protocols,我想要的是为某些NSObjectProtocol自动实现委托方法,它符合某些协议,但我努力了,没有完成 下面是演示 更新以获得更准确的信息 ========================================================================= 我得到了一个协议PagedLoadable来获取collectionView所需的信息,然后扩展NSObjectProtocol where Self:Delegatable,自动配置对象实现PagedLoada
PagedLoadable
来获取collectionView所需的信息,然后扩展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 {
}
创建协议//类SurveyDownloadHandler
protocol SurveyDownloadHandlerDelegate {
func surveyDownloadSuccessfully(notiExpireRemainingTime : Int)
func surveyDownloadConnectionFail()
}
class SurveyDownloadHandler: NSObject {
var delegate: SurveyDownloadHandlerDelegate! = nil
}
//用于将方法调用回类Adelegate.surveyDownloadSuccessfully(notiExpireRemainingTime)
//甲级
class A : UIViewController,SurveyDownloadHandlerDelegate{
let surveyDownloadHandlerObject : SurveyDownloadHandler = SurveyDownloadHandler()
@IBAction func onTapClick(sender: AnyObject) {
self.surveyDownloadHandlerObject.delegate = self
self.surveyDownloadHandlerObject.startDownloadingSurvey()
}
}
func surveyDownloadSuccessfully(notiExpireRemainingTime : Int)
{
}
func surveyDownloadConnectionFail()
{
}
}
您正在尝试实现对协议的扩展,但继承除外。经过一些实验,我可以用下面的方法消除你的错误
//: [Next](@next)
protocol PagedLoadable {
var count: Int { get }
}
protocol Delegatable: UICollectionViewDelegate, UICollectionViewDataSource {
}
extension Delegatable {
}
//Removed since code will not be able to resolve dependency
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,PagedLoadable, Delegatable {
var count: Int {
return 1
}
}
//或者你也可以这样做
extension vc: PagedLoadable, Delegatable {
var count: Int {
return 1
}
}
您遇到的错误是什么?类型“vc”不符合协议“UICollectionViewDataSource”为什么不定义可删除的直接扩展名?@findall您能告诉我吗?T_T@InvokerLaw很抱歉,我已经测试了我所说的内容,但编译器因segfault崩溃,即使它已通过测试。(我尝试了UICollectionViewDataSource*/}所需的扩展可删除{/*实现)我想要的是为一些协议制作一个运行时的“插件模块”,无需实现Hey@Girish Kolari,我已经更新了描述,请看一看itI did need/*where Self:Delegatable*/bcoz我需要将delegate设置为Delegatable对象如果它是纯swift对象,没有参考目标C运行时,你可以在where Self:Delegatable执行。我只是好奇-在你的vc代码中,对象是它的delegate对象-你将它设置为delegate对象有什么错误