[英]Typhoon won't inject into plain NSObject derived class
I have a Swift 3 app using Typhoon which successfully injects a NSObject-derived object into a property in several objects deriving from UIViewController. 我有一个使用Typhoon的Swift 3应用程序,该应用程序成功地将NSObject派生的对象注入到从UIViewController派生的几个对象的属性中。 I added code to inject the same object into an object that derives only from NSObject, and the property is always nil upon executing any code in that object.
我添加了将同一对象注入仅源自NSObject的对象的代码,并且在该对象中执行任何代码时,该属性始终为nil。
My assembly looks like this: 我的程序集如下所示:
class CoreComponents: TyphoonAssembly {
dynamic func appInitializer() -> Any {
return TyphoonDefinition.withClass(AppInitializer.self) {
(definition) in
definition!.injectProperty(Selector(("settings")), with: self.provideSettings())
}
}
dynamic func sideMenu() -> Any {
return TyphoonDefinition.withClass(SideMenuTableView.self) {
(definition) in
definition!.injectProperty(Selector(("settings")), with: self.provideSettings())
}
}
dynamic func mapViewController() -> Any {
return TyphoonDefinition.withClass(MapViewController.self) {
(definition) in
definition!.injectProperty(Selector(("settings")), with: self.provideSettings())
}
}
dynamic func provideSettings() -> Any {
return TyphoonDefinition.withClass(Settings.self) {
(definition) in
definition!.scope = TyphoonScope.singleton
}
}
} }
In that, only the injection specified by the first method fails to inject. 这样,只有第一种方法指定的注入无法注入。 The object it wants to inject into is declared like this:
要注入的对象声明如下:
class AppInitializer: NSObject {
var settings: Settings? // Injected property
// Other code here
}
Does anybody see anything I'm doing wrong? 有人看到我在做错什么吗?
Here is the relevant portion of a class where the injection works: 这是注入工作所在类的相关部分:
class SideMenuTableView: UITableViewController {
var settings: Settings? // Injected property
// Other code
}
Thanks! 谢谢!
To get this to work, I abandoned the Objective-C compatible version of Typhoon and switched to the "pure swift" version of Typhoon at their website . 为了使它生效,我放弃了与Objective-C兼容的Typhoon版本,并在其网站上切换到了“纯快速” Typhoon版本。 The setup is more work, but the code ends up simplified:
设置工作更多,但代码最终得到了简化:
class CoreComponents: Assembly {
func provideSettings() -> Definition {
return Definition(withClass: Settings.self) { d in
d.setScope(Definition.Scope.Singletone)
}
}
}
class AppInitializer: NSObject {
let settings = CoreComponents.assembly.provideSettings()
// More code here
}
That required instantiating the assembly in my AppDelegate's "didFinishLaunchingWithOptions" method with this line: 这需要使用以下代码行在我的AppDelegate的“ didFinishLaunchingWithOptions”方法中实例化该程序集:
Typhoon.activateAssemblies()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.