2016-06-25 33 views
0

我試圖想出一種方法來減少用於依賴注入的樣板代碼CleanseViewController s。但是,絕對卡在Cleanse.Component樣板文件的類型擦除類中。我究竟做錯了什麼?斯威夫特找不到方法保證的方法

(包括代碼,以及快照的XCode,您的閱讀方便)

import Foundation 
import Cleanse 

protocol CleanseInjectable { 
    func inject(args: Any...) -> Void 
    func prepare() -> Void 
} 

extension CleanseInjectable { 
    func inject(args: Any...) -> Void { 

    } 
} 

class AnyCleansePropertyInjectionComponent<TargetType: AnyObject, CleanseInjectable>: Cleanse.Component { 
    typealias Root = PropertyInjector<TargetType> 

    func configure<B : Binder>(binder binder: B) { 
     binder 
      .bindPropertyInjectionOf(TargetType.self) 
      .to(injector: (TargetType.self as! CleanseInjectable.Type).inject) 
    } 
} 

我得到的錯誤是因爲任何斯威夫特協議的錯誤,這只是明顯的在事後離奇:Type 'CleanseInjectable' has no member 'inject' ?!

XCode Snapshot

+0

這對Cleanse開發人員來說不是問題嗎? – matt

+0

@matt提出了一個新的標籤以防萬一 - 但是'CleanseInjectable'是我的協議(不是框架的)!看來Swift無法找到我在協議中保證的方法簽名 – Angad

+0

我的意思是,你不應該通過github上的Issues頁面來查詢嗎?這個東西是一個總測試版,不是嗎? – matt

回答

0

該版本似乎修復錯誤!看來Swift無法理解TargetType: AnyObject, CleanseInjectable約束相當於protocol CleanseInjectable: class

import Cleanse 

protocol CleanseInjectable: class { 
    func inject(args: Any...) -> Void 
    func prepare() -> Void 
} 

extension CleanseInjectable { 
    func inject(args: Any...) -> Void { 

    } 
} 

class AnyCleansePropertyInjectionComponent<TargetType: CleanseInjectable>: Cleanse.Component { 
    typealias Root = PropertyInjector<TargetType> 

    func configure<B : Binder>(binder binder: B) { 
     binder 
      .bindPropertyInjectionOf(TargetType.self) 
      .to(injector: TargetType.inject) 
    } 
} 

編輯:我完成了淨化鍋爐板卸妝(在運行時未經測試,將在最早的更新),爲樂趣和啓發!

import Cleanse 

protocol CleanseInjectable: class { 
    var cleanseComponent: AnyCleansePropertyInjectionComponent<Self>? { get } 
    func inject(args: Any...) -> Void 
    func prepare() -> Void 
} 

extension CleanseInjectable { 
    func inject(args: Any...) -> Void { 
     // Override me to do something useful, brah! 
    } 

    func prepare() -> Void { 
     if (cleanseComponent != nil) { 
      (try! cleanseComponent!.build()).injectProperties(into: self) 
     } 
    } 
} 

class AnyCleansePropertyInjectionComponent<TargetType: CleanseInjectable>: Cleanse.Component { 
    typealias Root = PropertyInjector<TargetType> 

    func configure<B : Binder>(binder binder: B) { 
     binder 
      .bindPropertyInjectionOf(TargetType.self) 
      .to(injector: TargetType.inject) 
    } 
}