Swinject 容器注册失败
Swinject container registration fails
在尝试将某些协议绑定到各种 classes 时,我遇到了在协议中获取 nil
的奇怪行为。
这是依赖注入器 class:
class DependencyInjector: NSObject {
let container: Container
override init() {
container = Container()
super.init()
register()
}
}
extension DependencyInjector {
func register() {
container.register(NetworkProtocols.self) { _ in ApiManager() }.inObjectScope(.container)
container.register(ConnectivityManager.self) { r in
let controller = ConnectivityManager()
controller.network = r.resolve(NetworkProtocols.self)
return controller
}
container.storyboardInitCompleted(ChooseCameraViewController.self) {r,c in
c.network = r.resolve(NetworkProtocols.self)
}
container.storyboardInitCompleted(InstallationViewController.self) {r,c in
c.network = r.resolve(NetworkProtocols.self)
}
container.storyboardInitCompleted(CameraSetupViewController.self) {r,c in
c.network = r.resolve(NetworkProtocols.self)
}
container.storyboardInitCompleted(LoginViewController.self) { r,c in
c.network = r.resolve(NetworkProtocols.self)
}
}
}
这是AppDelegate
:
var dependencyInjector: DependencyInjector!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
dependencyInjector = DependencyInjector()
let window = UIWindow(frame: UIScreen.main.bounds)
self.window = window
let bundle = Bundle(for: ViewController.self)
let storyboard = SwinjectStoryboard.create(name: "Main", bundle: bundle, container: dependencyInjector.container)
window.rootViewController = storyboard.instantiateInitialViewController()
return true
}
在下面的 class 中,我在协议 属性:
中得到 nil
class ConnectivityManager: NSObject {
var network: NetworkProtocols!
func connectHotSpot() {
self.network.whoAmI(success: {
print("api succses")
}, failure: { (error) in
print(error.localizedDescription)
})
}
}
网络变量始终是 nil
,而在使用 storyboardInitCompleted
的其他 class 中,网络变量有效。
我做错了什么?
正如@JakubVano 所说,我必须获得 ConnectivityManager
:
的一个实例
container.register(ConnectivityManager.self) { r in
let controller = ConnectivityManager()
controller.network = r.resolve(NetworkProtocols.self)
return controller
}
container.storyboardInitCompleted(ChooseCameraViewController.self) {r,c in
c.connectivityManager = r.resolve(ConnectivityManager.self)!
}
在尝试将某些协议绑定到各种 classes 时,我遇到了在协议中获取 nil
的奇怪行为。
这是依赖注入器 class:
class DependencyInjector: NSObject {
let container: Container
override init() {
container = Container()
super.init()
register()
}
}
extension DependencyInjector {
func register() {
container.register(NetworkProtocols.self) { _ in ApiManager() }.inObjectScope(.container)
container.register(ConnectivityManager.self) { r in
let controller = ConnectivityManager()
controller.network = r.resolve(NetworkProtocols.self)
return controller
}
container.storyboardInitCompleted(ChooseCameraViewController.self) {r,c in
c.network = r.resolve(NetworkProtocols.self)
}
container.storyboardInitCompleted(InstallationViewController.self) {r,c in
c.network = r.resolve(NetworkProtocols.self)
}
container.storyboardInitCompleted(CameraSetupViewController.self) {r,c in
c.network = r.resolve(NetworkProtocols.self)
}
container.storyboardInitCompleted(LoginViewController.self) { r,c in
c.network = r.resolve(NetworkProtocols.self)
}
}
}
这是AppDelegate
:
var dependencyInjector: DependencyInjector!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
dependencyInjector = DependencyInjector()
let window = UIWindow(frame: UIScreen.main.bounds)
self.window = window
let bundle = Bundle(for: ViewController.self)
let storyboard = SwinjectStoryboard.create(name: "Main", bundle: bundle, container: dependencyInjector.container)
window.rootViewController = storyboard.instantiateInitialViewController()
return true
}
在下面的 class 中,我在协议 属性:
中得到nil
class ConnectivityManager: NSObject {
var network: NetworkProtocols!
func connectHotSpot() {
self.network.whoAmI(success: {
print("api succses")
}, failure: { (error) in
print(error.localizedDescription)
})
}
}
网络变量始终是 nil
,而在使用 storyboardInitCompleted
的其他 class 中,网络变量有效。
我做错了什么?
正如@JakubVano 所说,我必须获得 ConnectivityManager
:
container.register(ConnectivityManager.self) { r in
let controller = ConnectivityManager()
controller.network = r.resolve(NetworkProtocols.self)
return controller
}
container.storyboardInitCompleted(ChooseCameraViewController.self) {r,c in
c.connectivityManager = r.resolve(ConnectivityManager.self)!
}