Swiftpack.co - Package - achernoprudov/SwiftDI


Travis CI CocoaPods Version License Platforms Swift Version Swift Package Manager compatible

Simple dependency injection library for swift projects.


Assemble dependencies

Basically SwiftDI works as Service locator. It stores all dependencies inside own list. But Injector can be easily splited into submodules to reduce code linking.

// Swift
let injector = Injector()

    .with { return RestApi() }

    .with { (api: ApiProtocol) -> RepositoryProtocol.self in
        return Repository(api: api) 

// or you can bind Repository constructor directly

Inject dependencies to ViewController directly

// Swift
class Controller: UIViewController {

  var injector: Injector! = ... // provide injector to VC
  var repository: RepositoryProtocol!

  func viewDidLoad() {
      repository = injector.resolve()

Dependency injection options

// Swift
injector.bind(ApiProtocol.self) // link with type
    .tag("some tag")            // link with custom string tag
    .scope(.singleton)          // define dependency as singleton
    .with { ... }               // initilization dependency closure
injector.resolve(ApiProtocol.self, tag: "some value") //resolving by type and tag 


Injector support modularization and could have isollated submodules that know about parent but don't know about siblings.

// Top level module
let injector = Injector()

    .with { injector -> RepositoryProtocol.self in
         return Repository()

// Screen module
let childInjector = injector.plus()
    .with { injector -> PresenterProtocol in 
        return Presenter(repository: injector.resolve())


CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. For setup you have to visit their website. Once you have set up your project for CocoaPods you can specify SwiftDI dependency in your Podfile:

pod 'SwiftDI', '~> 2.0'

Swift Package Manager

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Once you have your Swift package set up, adding SwiftDI as a dependency is as easy as adding it to the dependencies value of your Package.swift:

Swift 5

dependencies: [
    .package(url: "https://github.com/achernoprudov/SwiftDI.git", from: "2.0.0")


Stars: 1
Help us keep the lights on


Used By

Total: 0