Typhoon:从withFactory:selector:injection style中将子类属性注入到定义中

时间:2016-03-25 20:58:23

标签: ios swift typhoon

我正在使用Typhoon将依赖项注入UIViewController的子类。我找到了a different question i had的潜在解决方案,它涉及使用“工厂”注入方法实例化视图控制器:

return TyphoonDefinition.withFactory(self.storyboard(), selector:"instantiateViewControllerWithIdentifier:", parameters: { (method) in
            method.injectParameterWith("camera-mode-controller")
        }, configuration: { (definition) in
            definition.injectProperty("cameraProvider", with: self.systemComponents.systemCameraProvider())
    })

但是,UIStoryboard签名func instantiateViewControllerWithIdentifier(identifier: String) -> UIViewController表示此工厂方法初始值设定项将创建UIViewController,它不具有我的子类(dynamic var cameraProvider)的属性。因此,在运行时,属性注入失败并显示setter not found

有没有办法说出类似“使用类创建定义:和工厂:使用方法:和属性”,以便定义知道它生成的类不是UIViewController,但事实上,在我的情况下,CameraModeViewController: UIViewController?在我看到的API文档中,TyphoonDefinition.withParent:class:configuration:可以使用withFactory:selector:parameters:方法进行链接以产生这种效果?但是,我的尝试:

public dynamic func viewControllerFromID(id: String) -> AnyObject {
    return TyphoonDefinition.withFactory(self.storyboard(), selector: "instantiateViewControllerWithIdentifier:") {
        (method) in
        method.injectParameterWith(id)
    }
}

public dynamic func cameraModeViewController() -> AnyObject {
    return TyphoonDefinition.withParent(self.viewControllerFromID("camera-mode-controller"), `class`: CameraModeViewController.self) {
        (definition) in
        definition.injectProperty("cameraProvider", with: self.systemComponents.systemCameraProvider())
    }
}

'You can't call a method on the runtime argument being passed in. It has to be passed in as-is'中使用选择器参数-[TyphoonInjectionByRuntimeArgument forwardingTargetForSelector:]生成错误:"length"。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我为自己解决了这个问题:由于版本控制问题,带有该标识符的storyboard成员忘记了它的UIViewController子类赋值,因此实际上不是可转换的,并且没有该属性。事实证明,台风在注入视图控制器子类中声明的属性时效果很好。