import Swift_IoC_Container
protocol SuperAwesomeBot {
func saySomething()
}
class StarWarsBot: SuperAwesomeBot {
func saySomething() {
print("The Force will be with you. Always.")
}
}
And use it like this:
// only needed once
IoC.shared.registerLazySingleton(SuperAwesomeBot.self, { StarWarsBot() })
let myBot: SuperAwesomeBot = try! IoC.shared.resolve()
myBot.saySomething()
If you are using Swift 5.1 or newer, you could take advantage of the new property wrapper feature. It's already implemented in this library.
There is no more need to initialize the property manually in the initializer. Just declare the property with the expected protocol type.
@Injected private var myBot: SuperAwesomeBot
After declaring the property as @Injected
you can access the previously registered object as you are used to.
An example implementation can be found in Example.swift
There are no further requirements beside Swift 5.1, just copy this little lib in your project and start.
swift_ioc_container is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Swift-IOC-Container'
- First, do your changes :)
- Update PodSpec with a new version number
- Push and create a new tag with the same version number
- Execute the following line to update PodSpec on CocoaPods.org
pod trunk push Swift-IOC-Container.podspec
You need to be an owner of course.
- Create your account like this:
pod trunk register myEmail@mail.de "Mein Name" --description="Name meines Rechners"
- An owner needs to add you as owner:
pod trunk add-owner Swift-IOC-Container email@email.de
chmod 0600 ~/.netrc
Michael Scherbakow, michael.scherbakow@brickmakers.de
Jonas Ă–sterle, jonas.oesterle@brickmakers.de
Pascal Friedrich, pascal.friedrich@brickmakers.de
Philipp Manstein, philipp.manstein@brickmakers.de
Swift_IoC_Container is available under the MIT license. See the LICENSE file for more info.