DITranquillity
The small library for dependency injection in applications written on pure Swift for iOS/OSX/tvOS. Despite its size, it solves a large enough range of tasks, including Storyboard support. Its main advantage - modularity of support, detailed errors description and lots of opportunities.
Features
- Pure Swift Type Support
- Initializer/Property/Method Injections
- Named and Tags definitions
- Type forwarding
- Lifetimes: single, lazySingle, weakSingle, objectGraph, prototype
- iOS/macOS Storyboard and StoryboardReference
- Circular dependencies
- Three level hierarchy: types, part, framework
- Short resolve syntax
- Scan Parts/Frameworks
- Very detail logs
- Thread safe
Usage
See code
“`Swift // container - for register and resolve your types let container = DIContainer() container.register{ Cat(name: "Felix”) } .as(Animal.self) // register Cat with name felix by protocol Animal .lifetime(.prototype) // set lifetime container.register(PetOwner.init) // register PetOwner // you can validate you registrations if !container.valid() { fatalError(“…”) } …………………………………………. // get instance of a types from the container let owner: PetOwner = container.resolve() let animal: Animal = *container // short syntax print(owner.pet.name) // “Felix” print(animal.name) // “Felix” …………………………………………. // where protocol Animal { var name: String { get } } class Cat: Animal { let name: String init(name: String) { self.name = name } } class PetOwner { let pet: Animal init(pet: Animal) { self.pet = pet } } “`For more details:
- Read the Quick Start ru /
en - Or documentation ru /
en - Also see code documentation
Install
Via CocoaPods.
To install DITranquillity with CocoaPods, add the following lines to your Podfile: pod 'DITranquillity'
Via Carthage.
github "ivlevAstef/DITranquillity"
Swift (iOS8+,macOS10.10+,tvOS9+)
Requirements
iOS 8.0+,macOS 10.10+,tvOS 9.0+; ARC
- Swift 3.0-3.2: Xcode 8.0-9.0; version >= 0.9.5
- Swift 2.3: Xcode 7.0; version < 0.9.5
Migration
Changelog
See CHANGELOG.md file.
Storyboard (iOS/OS X)
See code
Create your ViewController: ”`Swift class ViewController: UIViewController/NSViewController { var inject: Inject? override func viewDidLoad() { super.viewDidLoad() print(“Inject: \(inject)”) } } “` Create container: ”`Swift let container = DIContainer() container.register(ViewController.self) .injection { $0.inject = $1 } “` Create Storyboard: ”`Swift /// for iOS func applicationDidFinishLaunching(_ application: UIApplication) { let storyboard = DIStoryboard.create(name: “Main”, bundle: nil, container: container) window = UIWindow(frame: UIScreen.main.bounds) window!.rootViewController = storyboard.instantiateInitialViewController() window!.makeKeyAndVisible() } “` ”`Swift /// for OS X func applicationDidFinishLaunching(_ aNotification: Notification) { let storyboard = DIStoryboard.create(name: “Main”, bundle: nil, container: container) let viewController = storyboard.instantiateInitialController() as! NSViewController let window = NSApplication.shared().windows.first window?.contentViewController = viewController } “`Alternative
Feedback
I’ve found a bug, or have a feature request
Please raise a GitHub issue.
I’ve found a defect in documentation, or thought up how to improve it
Please help library development and create pull requests
Question?
You can feel free to ask the question at e-mail: ivlev.stef@gmail.com.