我创建了一个超级简单的协议:
protocol IndependentProtocol {}
Run Code Online (Sandbox Code Playgroud)
和服务:
class IndependentService: IndependentProtocol {}
Run Code Online (Sandbox Code Playgroud)
以及以下Swinject注册工作:
defaultContainer.register( IndependentProtocol.self )
{
_ in IndependentService()
}
Run Code Online (Sandbox Code Playgroud)
但以下一项则不然:
defaultContainer.register( IndependentProtocol.self )
{
_ in IndependentService()
}.inObjectScope( .Container )
Run Code Online (Sandbox Code Playgroud)
给出的错误是:
Ambiguous reference to member 'register(_:name:factory:)'
Run Code Online (Sandbox Code Playgroud)
有趣的是,以下工作(即:带有参数的服务可以在.container范围内注册):
defaultContainer.register( AnotherProtocol.self )
{
r in AnotherService(
signals: r.resolve( AnotherService.self )!
)
}.inObjectScope( .container )
Run Code Online (Sandbox Code Playgroud)
我读过这个类似的问题,这没有帮助:Swinject - 对成员的模糊引用
谢谢大家。