2015-11-24 153 views
2

我想要的是一些NSObjectProtocol的自動實現委託方法符合某些協議,但我努力工作,沒有完成。Swift2協議擴展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 { 

} 
+0

什麼是錯誤您收到? –

+0

類型'vc'不符合協議'UICollectionViewDataSource' – InvokerLaw

+0

爲什麼不定義直接擴展到'Delegatable'? – findall

回答

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

    } 
} 
+0

我想要的是爲運行時的某些協議創建一個「插件模塊」,並且不需要實現 – InvokerLaw

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 
    } 
} 
+0

嘿@Girish Kolari,我一直更新的描述,請採取一個拿着它 – InvokerLaw

+0

我已經更新了我的答案... –

+0

我確實需要/ *自我:可委託*/bcoz我需要設置委託給可委託對象 – InvokerLaw