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.
- Pure Swift Type Support
- Injection with Arguments
- Initializer/Property/Method Injections
- Initialization Callback
- Circular Dependency Injection
- Object Scopes as None (Transient), Graph, Container (Singleton) and Hierarchy
- Support of both Reference and Value Types
- Self-registration (Self-binding)
- Container Hierarchy
- Thread Safety
- Modular Components
- SwinjectPropertyLoader: Loading property values from resources.
- SwinjectStoryboard: Automatic dependency injection via Storyboard.
- Swinject-CodeGen: Type-safe code generation of
Container
from a CSV/YAML file defining dependencies.
- iOS 8.0+ / Mac OS X 10.10+ / watchOS 2.0+ / tvOS 9.0+
- Swift 2.2 or 2.3
- Xcode 7.0+
- Swift 3
- Xcode 8.0+
- Carthage 0.18+ (if you use)
- CocoaPods 1.1.0.rc.2+ (if you use)
- When using Swinject for resolving ViewControllers, your
UIViewController
subclass must implement therequired init?(coder aDecoder: NSCoder)
initializer.
Swinject is available through Carthage or CocoaPods.
To install Swinject with Carthage, add the following line to your Cartfile
.
github "Swinject/Swinject" ~> 1.1.4
github "Swinject/Swinject" "2.0.0-beta.2"
# Uncomment if you use SwinjectStoryboard
# github "Swinject/SwinjectStoryboard" "1.0.0-beta.2"
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 Podfile
.
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0' # or platform :osx, '10.10' if your target is OS X.
use_frameworks!
pod 'Swinject', '~> 1.1.4'
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0' # or platform :osx, '10.10' if your target is OS X.
use_frameworks!
pod 'Swinject', '2.0.0-beta.2'
# Uncomment if you use SwinjectStoryboard
# pod 'SwinjectStoryboard', '1.0.0-beta.2'
Then run pod install
command. For details of the installation and usage of CocoaPods, visit its official website.
- Technical documents including patterns of dependency injection and examples.
- API reference
First, register a service and component pair to a Container
, where the component is created by the registered closure as a factory. In this example, Cat
and PetOwner
are component classes implementing AnimalType
and PersonType
service protocols, respectively.
let container = Container()
container.register(AnimalType.self) { _ in Cat(name: "Mimi") }
container.register(PersonType.self) { r in
PetOwner(pet: r.resolve(AnimalType.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(PersonType.self)!
person.play() // prints "I'm playing with Mimi."
Where definitions of the protocols and classes are
protocol AnimalType {
var name: String? { get }
}
class Cat: AnimalType {
let name: String?
init(name: String?) {
self.name = name
}
}
and
protocol PersonType {
func play()
}
class PetOwner: PersonType {
let pet: AnimalType
init(pet: AnimalType) {
self.pet = pet
}
func play() {
let name = pet.name ?? "someone"
print("I'm playing with \(name).")
}
}
Notice that the pet
of PetOwner
is automatically set as the instance of Cat
when PersonType
is resolved to the instance of PetOwner
. 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.
Services must be registered to a container before they are used. Typical ways of the registrations are different between the cases with/without SwinjectStoryboard
.
The following view controller class is used in addition to the protocols and classes above in the examples below.
class PersonViewController: UIViewController {
var person: PersonType?
}
Import SwinjectStoryboard at the top of your swift source file if you use Swinject v2 in Swift 3.
// Only Swinject v2 in Swift 3.
import SwinjectStoryboard
Services should be registered in an extension of SwinjectStoryboard
if you use SwinjectStoryboard
. Refer to the project page of SwinjectStoryboard for its details.
extension SwinjectStoryboard {
class func setup() {
defaultContainer.register(AnimalType.self) { _ in Cat(name: "Mimi") }
defaultContainer.register(PersonType.self) { r in
PetOwner(pet: r.resolve(AnimalType.self)!)
}
defaultContainer.register(PersonViewController.self) { r in
let controller = PersonViewController()
controller.person = r.resolve(PersonType.self)
return controller
}
}
}
Typically services are registered to a container in AppDelegate
if you do not use SwinjectStoryboard
to instantiate view controllers. If you register the services in AppDelegate
especially before exiting the call of application:didFinishLaunchingWithOptions:
, it is ensured that the services are registered before they are used.
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let container = Container() { c in
c.register(AnimalType.self) { _ in Cat(name: "Mimi") }
c.register(PersonType.self) { r in
PetOwner(pet: r.resolve(AnimalType.self)!)
}
c.register(PersonViewController.self) { r in
let controller = PersonViewController()
controller.person = r.resolve(PersonType.self)
return controller
}
}
func application(
application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Instantiate a window.
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.backgroundColor = UIColor.whiteColor()
window.makeKeyAndVisible()
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 Container
.
The project contains Sample-iOS.playground
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.
- SwinjectSimpleExample demonstrates dependency injection and Swinject in a simple weather app that lists current weather information at some locations.
- SwinjectMVVMExample demonstrates dependency injection with Swift and reactive programming with ReactiveCocoa in MVVM architecture.
The following blog posts introduce Swinject and the concept of dependency injection.
- Dependency Injection Framework for Swift - Introduction to Swinject
- Dependency Injection Framework for Swift - Simple Weather App Example with Swinject Part 1/2
- Dependency Injection Framework for Swift - Simple Weather App Example with Swinject Part 2/2
A guide to submit issues, to ask general questions, or to open pull requests is here.
If you have a general question and hesitate to submit an issue at GitHub, you can feel free to ask the question at Stack Overflow. The author of Swinject monitors swinject
tag there to answer as quickly as possible.
The DI container features of Swinject are inspired by:
and highly inspired by:
- Funq - Daniel Cazzulino and the project team.
MIT license. See the LICENSE file for details.