A really handy extension is the SwinjectStoryboard plugin, which adds Automatic Storyboard Injection to Swinject:
extension SwinjectStoryboard {
class func setup() {
defaultContainer.storyboardInitCompleted(WeatherTableViewController.self) { r, c in
c.weatherFetcher = r.resolve(WeatherFetcher.self)
}
defaultContainer.register(Networking.self) { _ in Network() }
defaultContainer.register(WeatherFetcher.self) { r in
WeatherFetcher(networking: r.resolve(Networking.self)!)
}
}
}
The SwinjectStoryboard class inherits from UIStoryboard and lets us inject all the dependencies in the view controllers defined in the storyboard. If the setup() class function is present, it will be called during the setup of the app, and it is the place where we'll bind the dependencies. Note that a defaultContainer property is defined in SwinjectStoryboard and this is what we use to set up the dependencies.
As we have seen, Swinject is very friendly and easy to use. Also, it isn't really invasive and it could be used only in certain parts of the app. SwinjectStoryboard is then a really nice add-on, which should help us clean the Injection in the view controller, an operation that usually is done in the prepareForSegue function of the calling view controller.