SwiftInjector 0.5.1

SwiftInjector 0.5.1

TestsTested βœ—
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2016
SPMSupports SPM βœ—

Maintained by Sash Zats.



Injector

Warning: this project is not considered to be feature complete.

Before:

let applicationController = ApplicationController(
    persistanceService: persistanceService,
    analyticsService: analyticsService,
    remoteConfigurationService: remoteConfigurationService,
    authenticationController: authenticationController,
    networkProvider: provider
)

After:

let applicationController = try injector.inject(ApplicationController.init)

Please check out playground included with project for more examples.

But wait, there is more

Simple injection:

let chicken = πŸ”()
injector.set(chicken)

// later in your code
let chicken: πŸ” = injector.get()
chicken.cockadoodle()

Explicit type specification

protocol πŸŒπŸ’• {
    func peel()
}

struct 🐡: πŸŒπŸ’• {
}

struct 🐨: πŸŒπŸ’• {
}

injector.set(🐡())
injector.set(🐨())

let banana: πŸŒπŸ’• = injector.get(type: 🐨.self)!
banana.peel()

Automatic injection

Injector can inject all the arguments for specified function using previously registered instances:

struct 🦁 {
    let heart: ❀️
    let innerKitten: 😺
    init(heart: ❀️, innerKitten: 😺) {
        self.heart = heart
        self.innerKitten = innerKitten
    }
}

injector.set(❀️())
injector.set(😺())

let lion = try injector.inject(🦁.init)
lion.meow()

If any of parameters is not registered with injector, it will throw an InjectorError.TypeNotFound(T) with first type injector failed to find. You are not limited to initializer, you can use any method (with up to 30 arguments)

Current Limitations

  • If your class has several initializers, inject will get confused. As a possible workaround, you can define a convinience class function that doesn’t have any
  • Complexity of get method is currently O(n). PRs on improving this one are very much welcome. The only limitation is not to use Objective-C runtime magic.

Thanks

  • gfontenot for the inspiration and the original implementation of Curry.

Installation