0

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...

Alon Shlider
  • 1,187
  • 1
  • 16
  • 46

0 Answers0