简体   繁体   English

带有Initalizer崩溃应用程序的台风参数注入

[英]Typhoon parameter injection with initalizer crashing app

i want to use Typhoon ( GitHub & WebSite ) for dependency injection in my app. 我想在我的应用程序中使用Typhoon( GitHubWebSite )进行依赖注入。 I use Swift Version 3 and Typhoon 3.6. 我使用Swift 3和Typhoon 3.6。 Unfortunately my app is crashing when I try to initalize an object. 不幸的是,当我尝试初始化对象时,我的应用程序崩溃了。 I have the following protocol: 我有以下协议:

Protocol 协议

import Foundation

@objc public protocol Client {

    func method()

}

Protocol implementation 协议实施

import Foundation

public class ClientWhateverImpl : NSObject, Client{

    let name : String

    init(name: name) {
        self.name = name
    }

    public func method(){
      //make something
    }

}

Assembly 部件

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
    }



}

Call it somewhere 叫某处

let myAssembly : MyAssembly = MyAssembly()
        myAssembly.activate()

let client = myAssembly.client()

Unfortunately I got the following error: 不幸的是,我收到以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Method 'initWithName:' not found on 'MyApp.ClientWhateverImpl'. Did you include the required ':' characters to signify arguments?'

I read some posts on stackoverflow about this error but on their side they forget to use the objectice-c method syntax. 我在stackoverflow上阅读了一些有关此错误的文章,但在他们身边,他们忘记了使用objectice-c方法语法。 But in my case I use the objc method "initWithName". 但就我而言,我使用objc方法“ initWithName”。 Is there something different in swift 3? 迅捷3有什么不同吗? Has someone the same problem? 有人有同样的问题吗?

Ok. 好。 I found the issue. 我发现了问题。 It has something to do with an object which i wanted to inject. 它与我要注入的对象有关。 It doesn't inherit from NSObject and Typhoon makes something with it and fails: 它不是从NSObject继承而来的,而Typhoon却使它失败了:

definition!.useInitializer("initWithObject:") {
    (initializer) in
    initializer!.injectParameter(with: MyObject())
}

Before: 之前:

public class MyObject{

}

Solution: 解:

public class MyObject: NSObject{

}

The documentation even says it: 该文档甚至说:

Every class you want to inject has to be a subclass of NSObject in some way (either by subclassing or adding @objc modifier). 您要注入的每个类都必须以某种方式成为NSObject的子类(通过子类化或添加@objc修饰符)。

I just thought the ClientWhateverImpl in my case has to inherit from NSObject . 我只是认为ClientWhateverImpl就必须继承自NSObject My fault. 我的错。 This is question closed 这个问题已经关闭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM