[英]Typhoon parameter injection with initalizer crashing app
我想在我的應用程序中使用Typhoon( GitHub和WebSite )進行依賴注入。 我使用Swift 3和Typhoon 3.6。 不幸的是,當我嘗試初始化對象時,我的應用程序崩潰了。 我有以下協議:
協議
import Foundation
@objc public protocol Client {
func method()
}
協議實施
import Foundation
public class ClientWhateverImpl : NSObject, Client{
let name : String
init(name: name) {
self.name = name
}
public func method(){
//make something
}
}
部件
import Foundation
import Typhoon
public class MyAssembly: TyphoonAssembly {
public dynamic func client() -> AnyObject {
return TyphoonDefinition.withClass(ClientWhateverImpl.self) {
(definition) in
definition!.useInitializer("initWithName:") {
(initializer) in
initializer!.injectParameter(with: "name")
}
} as AnyObject
}
}
叫某處
let myAssembly : MyAssembly = MyAssembly()
myAssembly.activate()
let client = myAssembly.client()
不幸的是,我收到以下錯誤:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Method 'initWithName:' not found on 'MyApp.ClientWhateverImpl'. Did you include the required ':' characters to signify arguments?'
我在stackoverflow上閱讀了一些有關此錯誤的文章,但在他們身邊,他們忘記了使用objectice-c方法語法。 但就我而言,我使用objc方法“ initWithName”。 迅捷3有什么不同嗎? 有人有同樣的問題嗎?
好。 我發現了問題。 它與我要注入的對象有關。 它不是從NSObject
繼承而來的,而Typhoon卻使它失敗了:
definition!.useInitializer("initWithObject:") {
(initializer) in
initializer!.injectParameter(with: MyObject())
}
之前:
public class MyObject{
}
解:
public class MyObject: NSObject{
}
該文檔甚至說:
您要注入的每個類都必須以某種方式成為NSObject的子類(通過子類化或添加@objc修飾符)。
我只是認為ClientWhateverImpl
就必須繼承自NSObject
。 我的錯。 這個問題已經關閉
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.