试图解析任何对象时,请始终返回零零
我的应用程序中有以下Swinject实现 -
import Foundation
import Swinject
@propertyWrapper
struct Inject<Component> {
let wrappedValue: Component
init() {
self.wrappedValue = Resolver.shared.resolve(Component.self)
}
}
class Resolver {
static let shared = Resolver()
private var container = buildContainer()
func resolve<T>(_ type: T.Type) -> T {
container.resolve(T.self)!
}
}
func buildContainer() -> Container {
let container = Container()
container.register(BasicApplicationNetworkService.self) { _ in
return BasicApplicationNetworkService()
}.inObjectScope(.container)
container.register(BasicApplicationViewModel.self) { resolver in
return BasicApplicationViewModel()
}.inObjectScope(.container)
container.register(BasicApplicationView.self) { resolver in
return BasicApplicationView()
}.inObjectScope(.container)
return container
}
@main
struct BasicApplicationApp: App {
@Inject
var statingView : BasicApplicationView
var body: some Scene {
WindowGroup {
statingView
}
}
}
在初始化时,我总是会在我的应用结构上拆下可选的值时出乎意料地找到一个。
无法弄清楚我缺少的东西 - 我有一个应该在运行时初始化的单例对象。解析器类中的某些内容似乎缺失了,但是在调试此代码时,它确实确实通过buildContainer()
方法,并将其注射原本应该初始化的内容 - 但仍在返回。似乎真的很明显,但这是上午0:44,希望有人更清醒了...
I have the following Swinject implementation in my app -
import Foundation
import Swinject
@propertyWrapper
struct Inject<Component> {
let wrappedValue: Component
init() {
self.wrappedValue = Resolver.shared.resolve(Component.self)
}
}
class Resolver {
static let shared = Resolver()
private var container = buildContainer()
func resolve<T>(_ type: T.Type) -> T {
container.resolve(T.self)!
}
}
func buildContainer() -> Container {
let container = Container()
container.register(BasicApplicationNetworkService.self) { _ in
return BasicApplicationNetworkService()
}.inObjectScope(.container)
container.register(BasicApplicationViewModel.self) { resolver in
return BasicApplicationViewModel()
}.inObjectScope(.container)
container.register(BasicApplicationView.self) { resolver in
return BasicApplicationView()
}.inObjectScope(.container)
return container
}
@main
struct BasicApplicationApp: App {
@Inject
var statingView : BasicApplicationView
var body: some Scene {
WindowGroup {
statingView
}
}
}
I always get a Unexpectedly found nil while unwrapping an Optional value
on my App struct when initialising.
Can't figure out what I am missing - I have a singleton object that should be initialized on runtime. Something in the Resolver class seems missing, but when debugging this code it does indeed go through the buildContainer()
method and intializes what it's supposed to initialise - but still nil is being returned. Seems like something really obvious but it's 0:44 AM so hopefully someone is more awake then I am...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论