https://github.com/1amageek/injectable
Dependency Injection for Swift
https://github.com/1amageek/injectable
dependency dependency-injection inject injection swift
Last synced: 10 months ago
JSON representation
Dependency Injection for Swift
- Host: GitHub
- URL: https://github.com/1amageek/injectable
- Owner: 1amageek
- License: mit
- Created: 2017-10-02T04:49:33.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-02T06:08:16.000Z (over 8 years ago)
- Last Synced: 2024-11-24T16:42:49.536Z (over 1 year ago)
- Topics: dependency, dependency-injection, inject, injection, swift
- Language: Ruby
- Homepage:
- Size: 106 KB
- Stars: 31
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](http://cocoapods.org/?q=Injectable)
[](http://cocoapods.org/?q=Injectable)
[](https://cocoapods.org/pods/Injectable)
# Injectable
Dependency Injection for Swift.
The dependency injection pattern leads to code that's modular and testable, and Guice makes it easy to write.
[Google Guice](https://github.com/google/guice/wiki/Motivation)
## Installation
```
pod 'Injectable'
```
## Usage
``` swift
// ViewController.swift
class ViewController: UIViewController {
@IBAction func show(_ sender: Any) {
let viewController: InjectableViewController = InjectableViewController(with: .init(color: .green))
self.present(viewController, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
```
``` swift
// InjectableViewController.swift
protocol Colorable: Injectable {
init(with dependency: Dependency)
}
class InjectableViewController: UIViewController, Colorable {
required init(with dependency: InjectableViewController.Dependency) {
super.init(nibName: nil, bundle: nil)
self.inject(dependency)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
struct Dependency {
var color: UIColor
}
func inject(_ dependency: InjectableViewController.Dependency) {
self.view.backgroundColor = dependency.color
}
}
```