A Swinject service class without parameters that cannot be registered in a .container scope with Swift 3.0: why?

I created a super simple protocol:

protocol IndependentProtocol {}

and service:

class IndependentService: IndependentProtocol {}

and the following works Swinject:

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

but the following:

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

given error:

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

and, interestingly, the following works (i.e. services with parameters can be registered in the area .container):

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

I read this similar question which did not help: Swinject - Ambiguous link to the participant

Thanks to everyone in advance.

+1
source share
1 answer

As Jakub commented, the problem is your capitalization .Container. Update your registration to the following:

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

Source: https://habr.com/ru/post/1656494/


All Articles