Ios Swift 3.0更新后Swinject中不同ViewController中的依赖项注入不一致:为什么?
我正在注册一些Ios Swift 3.0更新后Swinject中不同ViewController中的依赖项注入不一致:为什么?,ios,swift,swinject,Ios,Swift,Swinject,我正在注册一些Swinjectsingleton-with-a-small-s(.container)服务,因此: defaultContainer.register( SomeService.self ) { _ in SomeService() }.inObjectScope( .container ) defaultContainer.register( AnotherService.self ) { responder in AnotherService(
Swinject
singleton-with-a-small-s(.container)服务,因此:
defaultContainer.register( SomeService.self )
{
_ in SomeService()
}.inObjectScope( .container )
defaultContainer.register( AnotherService.self )
{
responder in AnotherService(
someService: responder.resolve( SomeService.self )!
)
}.inObjectScope( .container )
将它们注入某些视图控制器,从而:
defaultContainer.registerForStoryboard( SomeViewController.self )
{
resolvable, viewController in
viewController.someService = resolvable.resolve( SomeService.self )
viewController.anotherService = resolvable.resolve( AnotherService.self )!
}
defaultContainer.registerForStoryboard( AnotherViewController.self )
{
resolvable, viewController in
viewController.someService = resolvable.resolve( SomeService.self )
viewController.anotherService = resolvable.resolve( AnotherService.self )!
}
这些视图控制器将以两种不同的方式显示,SomeViewController
,如下所示:
DispatchQueue.main.async
{
self.performSegue( withIdentifier: "somePageSegue", sender: nil )
}
let anotherViewController = UIStoryboard(
name: "Main"
, bundle: nil
).instantiateViewController( withIdentifier: "anotherSegue" )
present( anotherViewController, animated: true, completion: nil )
和另一个视图控制器,如下所示:
DispatchQueue.main.async
{
self.performSegue( withIdentifier: "somePageSegue", sender: nil )
}
let anotherViewController = UIStoryboard(
name: "Main"
, bundle: nil
).instantiateViewController( withIdentifier: "anotherSegue" )
present( anotherViewController, animated: true, completion: nil )
SomeViewController
注入了它的服务,但不幸的是另一个viewcontroller
没有
在Swinject升级到Swift 3.0之前,这一功能曾经起作用,但现在不起作用。为什么会这样,还有什么需要改变
多谢各位
更新
不幸的是,我既不熟悉Swinject
的底层代码库,也没有时间熟悉自己,但通过深入了解表面下发生的事情,我发现了以下几点,希望对任何比我更了解它的人都有用:
成功的VC DI:
// once into:
private func injectDependency(to viewController: UIViewController)
// then:
defaultContainer.registerForStoryboard( SomeViewController.self )
// then many times into:
public func _resolve<Service, Factory>(name: String?, option: ServiceKeyOptionType? = nil, invoker: (Factory) -> Service) -> Service?
// repeatedly going from:
private func injectDependency(to viewController: UIViewController)
// to:
public func _resolve<Service, Factory>(name: String?, option: ServiceKeyOptionType? = nil, invoker: (Factory) -> Service) -> Service?
// and back again,
// then twice into:
public override func instantiateViewController(withIdentifier identifier: String) -> UIViewController
//一旦进入:
private func injectDependency(到viewController:UIViewController)
//然后:
defaultContainer.registerForStoryboard(SomeViewController.self)
//然后多次进入:
公共函数解析(名称:字符串?,选项:ServiceKeyOptionType?=nil,调用程序:(工厂)->服务)->服务?
失败的VC DI:
// once into:
private func injectDependency(to viewController: UIViewController)
// then:
defaultContainer.registerForStoryboard( SomeViewController.self )
// then many times into:
public func _resolve<Service, Factory>(name: String?, option: ServiceKeyOptionType? = nil, invoker: (Factory) -> Service) -> Service?
// repeatedly going from:
private func injectDependency(to viewController: UIViewController)
// to:
public func _resolve<Service, Factory>(name: String?, option: ServiceKeyOptionType? = nil, invoker: (Factory) -> Service) -> Service?
// and back again,
// then twice into:
public override func instantiateViewController(withIdentifier identifier: String) -> UIViewController
//重复从:
private func injectDependency(到viewController:UIViewController)
//致:
公共函数解析(名称:字符串?,选项:ServiceKeyOptionType?=nil,调用程序:(工厂)->服务)->服务?
//再回来,
//然后两次进入:
public override func instantialeviewcontroller(带标识符:String)->UIViewController
附加说明:
// once into:
private func injectDependency(to viewController: UIViewController)
// then:
defaultContainer.registerForStoryboard( SomeViewController.self )
// then many times into:
public func _resolve<Service, Factory>(name: String?, option: ServiceKeyOptionType? = nil, invoker: (Factory) -> Service) -> Service?
// repeatedly going from:
private func injectDependency(to viewController: UIViewController)
// to:
public func _resolve<Service, Factory>(name: String?, option: ServiceKeyOptionType? = nil, invoker: (Factory) -> Service) -> Service?
// and back again,
// then twice into:
public override func instantiateViewController(withIdentifier identifier: String) -> UIViewController
失败的VC是TabBarController中的UIViewController,两者都已在标准XCode情节提要中列出。为了调用
寄存器或脚本
,您需要从SwinjectStoryboard
实例化给定的控制器,而不仅仅是UIStoryboard
:
let anotherViewController = SwinjectStoryboard.create(name: "Main", bundle: nil)
.instantiateViewController(withIdentifier: "anotherSegue")
present(anotherViewController, animated: true, completion: nil)
原来这是一个bug,详细信息如下:。目前正在进行修复。一旦我知道更多,我会向你汇报 更新
这显然是固定的。谢谢@Jakub Vanu我现在就来试试。不幸的是,这并没有造成任何影响@Jakub Vano,但谢谢你的建议。你的代码中是否有
defaultContainer
。defaultContainer?你好@Jakub Vano。是的,我有:扩展SwinjectStoryboard{class func setup({defaultContainer.register…我所有的注册都在哪里?你们有没有为SwinjectStoryboard设置容器?