Swiftpack.co - ptrkstr/Injector as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by ptrkstr.
ptrkstr/Injector 2.0.1
A Swift package for simple dependency injection that also supports Swift UI previews.
⭐️ 6
🕓 3 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/ptrkstr/Injector.git", from: "2.0.1")




A Swift package for simple dependency injection that also supports Swift UI previews.

Features

  • :white_check_mark: Built upon the amazing Factory
  • ✅ Tiny footprint (Injector.swift and Factory.swift are the only source files).
  • ✅ Built for mocking in UnitTests, UITests and SwiftUI previews.
  • ✅ Unit Tested.
  • ✅ Cool logo.

Usage

Injector uses the concept of a syringe:

extension Syringe {

}

Filled with medicine:

extension Syringe {
  // Same mock for all environments
  static let person = Medicine<PersonType>(Person(), mock: Person_Mock())
  // Unique mock for all environments
  static let cat = Medicine<CatType>(
      Cat(), 
      unitTests: Cat_Mock("Mary"), 
      uiTests: Cat_Mock("Sam"), 
      swiftUIPreview: Cat_Mock("Kerry")
  )
  // See Medicine initializer for all combinations
}

And at runtime injected:

let person = Syringe.person.inject()
// or
let person = Syringe.person()
// or
@Inject(Syringe.person) var person

Performing a clean will reset the instances:

Syringe.clean()

Installation

SPM

https://github.com/ptrkstr/injector

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics