简体   繁体   中英

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

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

Typhoon.activateAssemblies()

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