简体   繁体   中英

Typhoon parameter injection with initalizer crashing app

i want to use Typhoon ( GitHub & WebSite ) for dependency injection in my app. I use Swift Version 3 and 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. But in my case I use the objc method "initWithName". Is there something different in swift 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:

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).

I just thought the ClientWhateverImpl in my case has to inherit from NSObject . My fault. This is question closed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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