如何从Swinject中的其他模块中解析

发布于 2025-01-24 11:46:01 字数 1731 浏览 2 评论 0原文

使用Tuist模块化了项目。我在数据模块和域模块上制作了RepositorityAssembly和UseCaseSembly。汇编器位于InjectManager中,并在App模块中的AppDelegate中运行。

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

另外,在执行indetccontainer.container.container.solve中的resolve时,显示以下错误。

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

当我打印InjectContainer.container时,

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

为什么会出现此错误?你能告诉我如何解决吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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