Swift2协议扩展NSObjectProtocol

时间:2015-11-24 06:07:45

标签: swift swift-protocols

我想要的是某些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 {

}

2 个答案:

答案 0 :(得分:0)

创建协议
// Class 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()
   {

  }
}

答案 1 :(得分:0)

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

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