Need help with Swinject?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

4.9K Stars 391 Forks MIT License 1.0K Commits 43 Opened issues


Dependency injection framework for Swift with iOS/macOS/Linux

Services available


Need anything else?

Contributors list


Travis CI Com Github Actions Carthage compatible CocoaPods Version License Platforms Swift Version Reviewed by Hound

Swinject is a lightweight dependency injection framework for Swift.

Dependency injection (DI) is a software design pattern that implements Inversion of Control (IoC) for resolving dependencies. In the pattern, Swinject helps your app split into loosely-coupled components, which can be developed, tested and maintained more easily. Swinject is powered by the Swift generic type system and first class functions to define dependencies of your app simply and fluently.




  • iOS 9.0+ / Mac OS X 10.10+ / watchOS 2.0+ / tvOS 9.0+
  • Xcode 10.2+
  • Swift 4.2+
  • Carthage 0.18+ (if you use)
  • CocoaPods 1.1.1+ (if you use)


Swinject is available through Carthage, CocoaPods, or Swift Package Manager.


To install Swinject with Carthage, add the following line to your

github "Swinject/Swinject"

Uncomment if you use SwinjectStoryboard

github "Swinject/SwinjectStoryboard"

Then run

carthage update --no-use-binaries
command or just
carthage update
. For details of the installation and usage of Carthage, visit its project page.


To install Swinject with CocoaPods, add the following lines to your

source ''
platform :ios, '9.0' # or platform :osx, '10.10' if your target is OS X.

pod 'Swinject'

Uncomment if you use SwinjectStoryboard

pod 'SwinjectStoryboard'

Then run

pod install
command. For details of the installation and usage of CocoaPods, visit its official website.

Swift Package Manager


add the following:
dependencies: [
    // Dependencies declare other packages that this package depends on.
    // .package(url: /* package url */, from: "1.0.0"),
    .package(url: "", from: "2.8.0")
targets: [
        name: "MyProject",
        dependencies: [..., "Swinject"]


Basic Usage

First, register a service and component pair to a

, where the component is created by the registered closure as a factory. In this example,
are component classes implementing
service protocols, respectively.
let container = Container()
container.register(Animal.self) { _ in Cat(name: "Mimi") }
container.register(Person.self) { r in
    PetOwner(pet: r.resolve(Animal.self)!)

Then get an instance of a service from the container. The person is resolved to a pet owner, and playing with the cat named Mimi!

let person = container.resolve(Person.self)! // prints "I'm playing with Mimi."

Where definitions of the protocols and classes are

protocol Animal {
    var name: String? { get }

class Cat: Animal { let name: String?

init(name: String?) { = name



protocol Person {
    func play()

class PetOwner: Person { let pet: Animal

init(pet: Animal) { = pet

func play() {
    let name = ?? "someone"
    print("I'm playing with \(name).")


Notice that the

is automatically set as the instance of
is resolved to the instance of
. If a container already set up is given, you do not have to care what are the actual types of the services and how they are created with their dependency.

Where to Register Services

Services must be registered to a container before they are used. The typical registration approach will differ depending upon whether you are using

or not.

The following view controller class is used in addition to the protocols and classes above in the examples below.

class PersonViewController: UIViewController {
    var person: Person?

With SwinjectStoryboard

Import SwinjectStoryboard at the top of your swift source file.

import SwinjectStoryboard

Services should be registered in an extension of

if you use
. Refer to the project page of SwinjectStoryboard for further details.
extension SwinjectStoryboard {
    @objc class func setup() {
        defaultContainer.register(Animal.self) { _ in Cat(name: "Mimi") }
        defaultContainer.register(Person.self) { r in
            PetOwner(pet: r.resolve(Animal.self)!)
        defaultContainer.register(PersonViewController.self) { r in
            let controller = PersonViewController()
            controller.person = r.resolve(Person.self)
            return controller

Without SwinjectStoryboard

If you do not use

to instantiate view controllers, services should be registered to a container in your application's
. Registering before exiting
will ensure that the services are setup appropriately before they are used.
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    let container: Container = {
        let container = Container()
        container.register(Animal.self) { _ in Cat(name: "Mimi") }
        container.register(Person.self) { r in
            PetOwner(pet: r.resolve(Animal.self)!)
        container.register(PersonViewController.self) { r in
            let controller = PersonViewController()
            controller.person = r.resolve(Person.self)
            return controller
        return container

func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

    // Instantiate a window.
    let window = UIWindow(frame: UIScreen.main.bounds)
    self.window = window

    // Instantiate the root view controller with dependencies injected by the container.
    window.rootViewController = container.resolve(PersonViewController.self)

    return true


Notice that the example uses a convenience initializer taking a closure to register services to the new instance of


Play in Playground!

The project contains

to demonstrate the features of Swinject. Download or clone the project, run the playground, modify it, and play with it to learn Swinject.

To run the playground in the project, first build the project, then select

Editor > Execute Playground
menu in Xcode.

Example Apps

Some example apps using Swinject can be found on GitHub.

Blog Posts

The following blog posts introduce the concept of dependency injection and Swinject.

Thanks the authors!

Contribution Guide

A guide to submit issues, to ask general questions, or to open pull requests is here.



The DI container features of Swinject are inspired by:

and highly inspired by:


MIT license. See the LICENSE file for details.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.