试图解析任何对象时,请始终返回零零

发布于 2025-01-29 11:47:02 字数 1465 浏览 2 评论 0原文

我的应用程序中有以下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文