Search code examples
swifttyphoon

Is it possible to register against a protocol in Typhoon for use with TyphoonComponentFactory.componentForType in swift


TyphoonComponentFactoryProtocol.componentForType(classOrProtocol: AnyObject!) cannot accept a protocol

As far as I can tell MyProtocol.self returns a Protocol and protocol does not seem to implement the AnyObject protocol (only Any).

Is there a way to use Typhoon for service location based upon a protocol in swift (other than just directly calling the assembly function) or is this just a limitation of swift and typhoon currently?


Solution

  • Unfortunately it appears that this method does not work for Swift, even for @objc protocols. It works fine for Objective-C. I've bumped the bug report, to at the very least, report that the method is unavailable in Swift.

    While its possible, in Typhoon its generally not necessary to resolve components using the TyphoonComponentFactory interface, as they can be resolved from the assembly, example:

    let assembly = SwiftMiddleAgesAssembly().activate()
    let knight = assembly.basicKnight() as! Knight 
    

    And if you wish you can to proceed from one Typhoon built object to another, the assembly can be injected.