Swiftpack.co -  nerdzlab/NerdzInject as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Simple dependency injection module on Swift language
.package(url: "https://github.com/nerdzlab/NerdzInject.git", from: "1.1.0")


NerdzInject library allow to easily use Dependency Injection patter in your Swift project.



You will need to register your instances or closures in NerdzInject class.

Registering instance

NerdzInject.shared.registerObject(_ object: myInstance)

Registering instance with specifying class type

This method should be used when you want to registed some inherited class instance for use when somebody will be resolving base class

NerdzInject.shared.registerObject(_ object: inheritedClassInstance, for: BaseClass.self)

Registering instance with specifying identifier

This method should be used when you want to registed some instance for use when somebody will be resolving by identifier

NerdzInject.shared.registerObject(_ object: myInstance, for: "my_custom_identifier")

Registering with closure

You can also register a closures that should return a value. This will allow you to implement factory or lazy initialization. Use singleton parameter to define if this needs to be a factory or lazy initialization. By default value is false.

NerdzInject.shared.register {

NerdzInject.shared.register(for: MyClass.self) {

NerdzInject.shared.register(singleton: true, for: "custom_identifier") {

Resoving instances

You can resove an instance in a similar ways like registering: by identifier, by specifying class, by instance type.

let resolvedByInstanceType: MyType? = NerdzInject.shared.resolve()
let resolvedByProvidedType: NyBaseType? = NerdzInject.shared.resolve(by: MyType.self)
let resolvedByIdentifier: MyType? = NerdzInject.shared.resolve(by: "identifier")

Or resolving by property wrapper Inject in similar ways.

@Inject var resolvedByInstanceType: MyType?
@Inject(MyType.self) var resolvedByProvidedType: MyBaseType?
@Inject("identifier") var resolvedByIdentifier: MyType?


You can use ForceInject property wrapper to avoid optional types on resolving. From previous example we will get:

@ForceInject var resolvedByInstanceType: MyType
@ForceInject(MyType.self) var resolvedByProvidedType: MyBaseType
@ForceInject("identifier") var resolvedByIdentifier: MyType

Removing instances

You can also remove registered instances.closures by using remove method

let success = NerdzInject.shared.remove(by: identifier)
NerdzInject.shared.remove(by: MyClass.self)

Installation Swift Package Manager

To add NerdzInject to a Swift Package Manager based project, add:

.package(url: "https://github.com/nerdzlab/NerdzInject")


This code is distributed under the MIT license. See the LICENSE file for more info.


Stars: 2
Last commit: 5 weeks 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