Swin is a library of service locators to help you use IoC in your projects
- iOS 10.0+ / Mac OS X 10.10+
- Xcode 12+
Swin is available through CocoaPods. To install it, simply add the following line to your Podfile:
platform :ios, '10.0'
use_frameworks!
pod 'Swin'
Use pod install
command. For more info of the installation and usage of CocoaPods, CocoaPods website.
We created the next Animal
protocol:
protocol Animal {
var name: String {get set}
func say() -> String
}
After that we have the Dog
struct:
struct Dog: Animal {
var name: String
init(name: String) {
self.name = name
}
func say() -> String {
"Guau Guau"
}
}
Also we have the cat
struct:
struct Cat: Animal {
var name: String
init(name: String) {
self.name = name
}
func say() -> String {
"Miau Miau"
}
}
Then we have the struct PetOwner, we assign the values inside in PetOwner
using the annotation @Inject
struct PetOwner {
@Inject var dog: Animal
@Inject var cat: Cat
}
We created the demoModule Module:
let demoModule = module { container in
container.factory { Dog(name: "Fido") as Animal}
container.factory { Cat(name: "Tiger")}
}
We in the AppDelegate let's star Swin
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Swin.modules(demoModule)
Swin.start()
/* some code */
return true
}
Oswaldo Leon, oswaldo.leon9@gmail.com
Swin is available under the MIT license. See the LICENSE file for more info.