安装
SwiftDiFramework 包含外部依赖 RxSwift。
以下是目前支持的安装选项
手动
SwiftDi 框架
CocoaPods
# Podfile
use_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'SwiftDiFramework'
end
替换 YOUR_TARGET_NAME
,然后在 Podfile
目录下,键入
$ pod install
使用
类
import SwiftDiFramework
final class A: InjectedType {
init() {}
@Inject var b: B!
}
final class B: InjectedType {
init() {}
@Inject var c: C!
func foo() -> String {
return "class B"
}
}
final class C: InjectedType {
init() {}
@Inject var d: D!
}
final class D: InjectedType {
init() {}
}
应用模块
import SwiftDiFramework
let appDiModule = AppComponent {
appSingeltons
appModules
}
let appSingeltons = singelton {
A.self
B.self
}
let appModules = module {
fun(type: C.self) {
return C()
}
fun(type: D.self) {
return D(name: "Foo")
}
}
应用删除代理
import SwiftDiFramework
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
...
SwiftDi.appComponent = appDiModule
...
return true
}
视图控制器
import SwiftDiFramework
@Inject var a: A!
override func viewDidLoad() {
super.viewDidLoad()
print(a.b.foo())
}