首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift2协议扩展NSObjectProtocol

Swift2协议扩展NSObjectProtocol
EN

Stack Overflow用户
提问于 2015-11-24 14:07:45
回答 2查看 540关注 0票数 2

我想要的是为一些符合某些协议的NSObjectProtocol自动实现委托方法,但我努力了,但没有做到。

下面是演示

更新以获得更准确

=========================================================================

我得到了一个协议PagedLoadable来获取collectionView所需的信息,然后是extension NSObjectProtocol where Self: Delegatable,对象实现PagedLoadable的自动配置

代码语言:javascript
复制
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 {

}
EN

回答 2

Stack Overflow用户

发布于 2015-11-24 14:28:37

创建协议

//类SurveyDownloadHandler

代码语言:javascript
复制
protocol SurveyDownloadHandlerDelegate {

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


class SurveyDownloadHandler: NSObject  {

    var delegate: SurveyDownloadHandlerDelegate! =  nil
}

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

// A类

代码语言:javascript
复制
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()
   {

  }
}
票数 0
EN

Stack Overflow用户

发布于 2015-11-24 15:09:23

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

代码语言:javascript
复制
    //: [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
        }
    }

//一种方式是

代码语言:javascript
复制
    class vc: UIViewController,PagedLoadable, Delegatable {
        var count: Int {
            return 1
        }

    }

//或者您也可以这样做

代码语言:javascript
复制
extension vc: PagedLoadable, Delegatable {
    var count: Int {
        return 1
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33886543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档