The Wayback Machine - https://web.archive.org/web/20200917172438/https://github.com/1amageek/Injectable
Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 

README.md

Version Platform Downloads

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

Installation

pod 'Injectable'

Usage

// 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()
    }
}
// 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
    }
}
You can’t perform that action at this time.