0

Modularized the project using Tuist. I made RepositoryAssembly and UseCaseAssembly on the Data module and Domain module. Assembler is located in InjectManager and runs in AppDelegate in the App module.

// InjectManager.swift
public struct InjectManager {
    public static func inject(container: Container) -> Assembler {
        return Assembler([
            RepositoryAssembly(),
            UseCaseAssembly()
        ], container: container)
    }
}
 ...
private var assembler: Assembler!
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        assembler = InjectManager.inject(container: InjectContainer.container)
        return true
    }
 ...

Also, when performing InjetcContainer.container.resolve in MainFeature module, the following error is displayed.

Swinject: Resolution failed. Expected registration:
    { Service: FetchRankingListUseCase, Factory: Resolver -> FetchRankingListUseCase }
Available registrations:
MainFeature/MainInteractor.swift:41: Fatal error: Unexpectedly found nil while unwrapping an Optional value
2022-04-26 23:36:21.127977+0900 GRIG[5952:4943332] MainFeature/MainInteractor.swift:41: Fatal error: Unexpectedly found nil while unwrapping an Optional value

When I print the InjectContainer.container,

[
    { Service: FetchRankingListUseCase, Factory: Resolver -> FetchRankingListUseCase, ObjectScope: graph },
    { Service: GRIGRepository, Factory: Resolver -> GRIGRepository, ObjectScope: graph }
]

Why is this error coming out? Could you tell me how to solve it?

baegteun
  • 3
  • 2
  • you only register an `Assembler` ? – Etienne Jézéquel Apr 27 '22 at 06:54
  • ```swift public struct UseCaseAssembly: Assembly { public func assemble(container: Container) { container.register(FetchRankingListUseCase.self) { r in FetchRankingListUseCase(grigRepository: r.resolve(GRIGRepository.self)!) } } public init() {} } ``` ```swift public struct RepositoryAssembly: Assembly { public func assemble(container: Container) { container.register(GRIGRepository.self) { _ in GRIGRepositoryImpl() } } public init() {} } ``` Are you talking about this? – baegteun Apr 27 '22 at 07:16
  • yes thank you, so this is called on the init of your Assembler ? with the same container ? Are you sure that you try to resolve your object from the correct Container ? – Etienne Jézéquel Apr 27 '22 at 07:48
  • When I prin(InjectContainer.container) before resolve ```swift [ { Service: FetchRankingListUseCase, Factory: Resolver -> FetchRankingListUseCase, ObjectScope: graph }, { Service: GRIGRepository, Factory: Resolver -> GRIGRepository, ObjectScope: graph } ] ``` It became a register in the container. I did it in InjectContainer.container when I resolve. So the container is correct – baegteun Apr 27 '22 at 08:17
  • how do you define `InjectContainer.container` ? – Etienne Jézéquel Apr 27 '22 at 08:33
  • ```swift import Swinject public struct InjectContainer { public static let container = Container() } ``` in 'ThirdPartyLib' module's 'InjectContainer.swift' file – baegteun Apr 27 '22 at 08:35

0 Answers0