2

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

}
4

2 回答 2

0

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

  }
}
于 2015-11-24T06:28:37.140 回答
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
    }
}
于 2015-11-24T07:09:23.053 回答