Search code examples
swiftswinject

Swinject service class without parameters failing to register in .container scope with Swift 3.0: why?


I've created a super simple protocol:

protocol IndependentProtocol {}

and service:

class IndependentService: IndependentProtocol {}

and the following Swinject registration works:

defaultContainer.register( IndependentProtocol.self )
{ 
    _ in IndependentService()
}

but the following one does not:

defaultContainer.register( IndependentProtocol.self )
{ 
    _ in IndependentService()
}.inObjectScope( .Container )

error given is:

Ambiguous reference to member 'register(_:name:factory:)'

and interestingly, the following works (ie: services with parameters can be registered in .container scope):

    defaultContainer.register( AnotherProtocol.self )
    {
        r in AnotherService(
            signals: r.resolve( AnotherService.self )!
        )
    }.inObjectScope( .container )

I have read this similar question, which did not help: Swinject - Ambiguous reference to member

Thank you all in advance.


Solution

  • As Jakub has commented, the issue lies with your capitalization of .Container. Update the registration to the following:

    defaultContainer.register(IndependentProtocol.self, factory: { _ in 
        IndependentService()
    }).inObjectScope(.container)