Closed vchmezov closed 7 years ago
I'm no home and can't chech this code. It's perhaps bug, But you can use:
class AppComponent: DIComponent{
var scope: DIComponentScope { return .public }
func load(builder: DIContainerBuilder){
builder.register(type: UIStoryboard.self)
.set(name: "Main")
.lifetime(.single)
.initial { UIStoryboard(name: "Main", bundle: nil) } // !!!!
builder.register(vc: StartupVC.self)
.initial(useStoryboard: {container in try container.resolve(name: "Main")}, identifier: "startup")
}
}
Or
class AppComponent: DIComponent{
var scope: DIComponentScope { return .public }
func load(builder: DIContainerBuilder){
builder.register(type: UIStoryboard.self)
.set(name: "Main")
.lifetime(.single)
.initial { DIStoryboard(name: "Main", bundle: nil, container: $0) } // short: initial(name: "Main", bundle: nil)
builder.register(vc: StartupVC.self)
}
} ... let storyboard: UIStoryboard= try! container.resolve(name: "Main") let viewController = storyboard.instantiateViewController(identifier: "startup")
The second option is preferable. In the evening of Sunday I can say more precisely.
Write from phone...
This works and properly injects view controller properties (not shown in my sample):
let storyboard: UIStoryboard= try! container.resolve(name: "Main")
let viewController = storyboard.instantiateViewController(identifier: "startup")
But it makes me to remember the view controllers and storyboards identifiers. It would be fine if I could keep all the identifiers in the injection components only. Hope the bug is fixable because your library is much more convenient than Typhoon for Swift application.
of course
I fix this bug, and write tests. See version 2.3.0.
Hello! App library crashes when I'm trying to instantiate view controller from container
Here is a component:
AppDelegate:
Crash is on this line let viewController = try! container.resolve(StartupVC.self)
fatal error: 'try!' expression unexpectedly raised an error: Cannot found initial method with signature: (DIContainer) throws -> Any. Use: Register type: StartupVC in file: AppComponent.swift on line: 23
Is it a bug or am I doing something wrong? I can send you sample project.