0

I've tried Swinject(SwinjectStoryboard) and got a problem. Please help me.

extension SwinjectStoryboard {

   @objc class func setup() {
      defaultContainer.register(SomeClass.self) { _ in
            SomeClass()
      }

      defaultContainer.storyboardInitCompleted(FirstViewController.self) { r, c in
           c.someClass = r.resolve(SomeClass.self)
      }

      defaultContainer.storyboardInitCompleted(SecondViewController.self) { r, c in
           c.someClass = r.resolve(SomeClass.self)
      }
   }
}

After adding dependencies I have different objects (instances) of SomeClass in FirstViewController and SecondViewController.

Thank you in advance!

Kazunori Takaishi
  • 2,268
  • 1
  • 15
  • 27
Ragara
  • 21
  • 5

1 Answers1

2

You need to change the registration of SomeClass to:

defaultContainer.register(SomeClass.self) { _ in SomeClass() }
    .inObjectScope(.container)

You can learn more about the object scopes in Swinject docs.

Jakub Vano
  • 3,833
  • 15
  • 29