【问题标题】:Swift2 protocol extension NSObjectProtocolSwift2 协议扩展 NSObjectProtocol
【发布时间】: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


【解决方案1】:

创建协议
//类SurveyDownloadHandler

protocol SurveyDownloadHandlerDelegate {

     func surveyDownloadSuccessfully(notiExpireRemainingTime : Int)
     func surveyDownloadConnectionFail()
}


class SurveyDownloadHandler: NSObject  {

    var delegate: SurveyDownloadHandlerDelegate! =  nil
}

//用于将方法调用回A类delegate.surveyDownloadSuccessfully(notiExpireRemainingTime)

// A类

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()
   {

  }
}

【讨论】:

  • 我想要的是为一些带有运行时的协议制作一个“插件模块”,不需要实现
【解决方案2】:

您正在尝试实现对协议的扩展,但继承除外。经过一些实验,我可以通过以下方法消除您的错误。

    //: [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
    }
}

【讨论】:

  • 嘿@Girish Kolari,我已经更新了描述,请看一下
  • 我确实需要 /*where Self: Delegable*/ bcoz 我需要将委托设置为可委托对象
  • 如果它是纯 swift 对象且不参考 Objective C 运行时,您可以在哪里执行 Self: Delectable。
  • 只是好奇-在您的 vc 代码对象中是它的委托对象-您必须将其设置为委托对象的错误是什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
相关资源
最近更新 更多