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

Features

  • ✅ Tiny footprint (Injector.swift and Resolver.swift are the only source files).
  • ✅ Supports SwiftUI previews.
  • ✅ Great unit tests.
  • ✅ Cool logo.

Usage

import Injector

// AppDelegate or App (SwiftUI)
Injector.setup {
    $0.register(Person(), mock: Person_Mock(), for: PersonType.self)
    $0.register(Cat(), mock: Cat_Mock(), for: CatType.self)
}

// App
let person = inject(PersonType.self) // Person
let cat = inject(CatType.self) // Cat

// Unit Test or SwiftUI Preview
let person = inject(PersonType.self) // Person_Mock
let cat = inject(CatType.self) // Cat_Mock

Why should my main app contain the mocks?

This is so mocks can be used in SwiftUI previews. I couldn’t find an alternative way but would be glad to hear it if you had one.

Installation

SPM

Add the following to your project:

https://github.com/ptrkstr/injector

GitHub

View Github