【问题标题】:How to return the same instance every time when resolving object with Swinject?每次使用 Swinject 解析对象时如何返回相同的实例?
【发布时间】:2018-10-08 13:42:07
【问题描述】:

目前我这样做:

    container.register(TabBarViewModelable.self) { (_, tabBarItems: [TabBarItemType]) in
        return TabBarViewModel(tabBarItems: tabBarItems)
    }.inObjectScope(.container)

它不起作用。有没有办法在我第一次用标签栏项目数组初始化它后每次都返回相同的实例?

【问题讨论】:

  • 你可以将 TabBarViewModel 设为单例并返回

标签: ios swift swinject


【解决方案1】:

您可以使用 singleton 模式。

基本上,您可以将 TabBarViewModel 包装在另一个单例类中并返回该类的对象。

看看

https://medium.com/@nimjea/singleton-class-in-swift-17eef2d01d88

【讨论】:

  • 非常非常糟糕的主意;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-08
相关资源
最近更新 更多