Swiftpack.co -  JARMourato/Injection as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Lightweight dependency injection framework
.package(url: "https://github.com/JARMourato/Injection.git", from: "1.1.0")


Build Status codebeat badge codecov Platforms

Injection is a tiny utility to help managing dependency injection.


  • Singleton, dependencies that are only instanciated once and are shared amongst its' users
  • Factory, dependencies that are instancied upon usage, unique to each user
  • Utility property wrappers


Swift Package Manager

If you're working directly in a Package, add Injection to your Package.swift file

dependencies: [
    .package(url: "https://github.com/JARMourato/Injection.git", .upToNextMajor(from: "1.0.0" )),

If working in an Xcode project select File->Swift Packages->Add Package Dependency... and search for the package name: Injection or the git url:



  1. Define dependencies:
import Injection

// A dependency that gets created every time it needs to be resolved, and therefore its lifetime is bounded to the instance that uses it
let reader = factory { RSSReader() }

// A dependency that gets created only once, the first time it needs to be resolved and has the lifetime of the application.
let database = singleton { Realm() }

// Syntatic sugar to combine dependencies to inject
let cacheModule = module {
    singleton { ImageCache() }
    factory { AudioCache() }
    factory { VideoCache() }
  1. Inject the dependencies before the application is initialized:
import Injection

class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        do {
            try inject {
        } catch {
            print("TODO: Handle error properly... \(error)")
        return true
  1. Use the injected dependencies using the provided property wrappers:
import Injection

class VideoPlayer: BackendProtocol {
    // Will resolve the dependency immediately upon type instantiation
    @Inject var database: Database
    // The dependency only gets resolved on the first time the property gets accessed
    @LazyInject var videoCache: VideoCache
    // The functionality is similar to `LazyInject` except the property may or may not have been injected.
    @OptionalInject var network: Network?

or via the initializer:

import Injection

struct Reader {
    private let rssReader: RSSReader
    init(rssReader: RSSReader = resolve()) {
        self.rssReader = rssReader


If you feel like something is missing or you want to add any new functionality, please open an issue requesting it and/or submit a pull request with passing tests 🙌




João (@_JARMourato)


Stars: 1
Last commit: 3 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

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