Typhoon + Swift:使用运行时参数时崩溃

时间:2015-09-15 07:31:35

标签: ios swift code-injection swift2 typhoon

我目前正在尝试使用Typhoon将依赖注入添加到我的应用程序中,这是现在的一半Swift,一半是传统的Objective-C。我试图从我的程序集中的Storyboard实例化一个视图控制器,但我正在下面的崩溃中运行。我在这里阅读https://github.com/appsquickly/Typhoon/issues/293使用NSString可以解决这个问题,但正如你在下面我所看到的那样,我已经做到了这一点并且不太有帮助...任何想法?

dynamic func typhoonStoryboard(name: NSString) -> AnyObject {
        return TyphoonDefinition.withClass(TyphoonStoryboard.self, configuration: { (definition) -> Void in
            definition.useInitializer("storyboardWithName:factory:bundle:", parameters: { (initializer) -> Void in
                initializer.injectParameterWith(name)
                initializer.injectParameterWith(self)
                initializer.injectParameterWith(NSBundle.mainBundle())
            })
        })
    }

 dynamic func viewController(withStoryboardName storyboardName: NSString, storyboardIdentifier: NSString) -> AnyObject {
        return TyphoonDefinition.withFactory(self.typhoonStoryboard(NSString(string: storyboardName)), selector: "instantiateViewControllerWithIdentifier:", parameters: { (method) -> Void in
            method.injectParameterWith(NSString(string: storyboardIdentifier))
        }, configuration: { (definition) -> Void in

        })
    }

知道可能出现什么问题吗?就像你看到我已经在使用NSString进行解决方法但它仍然因以下异常异常而崩溃...

2015-09-15 09:26:32.484 MyApp[64855:5393006] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You can't call a method on the runtime argument being passed in. It has to be passed in as-is'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010ae0ff65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010a42edeb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010ae0fe9d +[NSException raise:format:] + 205
    3   MyApp                            0x00000001052282b3 -[TyphoonInjectionByRuntimeArgument forwardingTargetForSelector:] + 67
    4   CoreFoundation                      0x000000010ad65c4e ___forwarding___ + 158
    5   CoreFoundation                      0x000000010ad65b28 _CF_forwarding_prep_0 + 120
    6   libswiftCore.dylib                  0x000000010bff54b3 _TTSf4g_d___TFSSCfMSSFT12_cocoaStringPSs9AnyObject__SS + 131
    7   libswiftCore.dylib                  0x000000010bfbce33 _TFSSCfMSSFT12_cocoaStringPSs9AnyObject__SS + 19
    8   libswiftFoundation.dylib            0x000000010c3e0af9 _TToFE10FoundationCSo8NSStringcfMS0_FT6stringS0__S0_ + 41
    9   libswiftFoundation.dylib            0x000000010c3e0a3a _TFE10FoundationCSo8NSStringCfMS0_FT6stringS0__S0_ + 58
    10  MyApp                            0x0000000104cc890d _TFC8MyApp22ViewControllerAssembly14viewControllerfS0_FT18withStoryboardNameCSo8NSString20storyboardIdentifierS1__PSs9AnyObject_ + 157
    11  MyApp                            0x0000000104cc90cf _TToFC8MyApp22ViewControllerAssembly14viewControllerfS0_FT18withStoryboardNameCSo8NSString20storyboardIdentifierS1__PSs9AnyObject_ + 79
    12  CoreFoundation                      0x000000010acfe85c __invoking___ + 140
    13  CoreFoundation                      0x000000010acfe6ae -[NSInvocation invoke] + 286
    14  CoreFoundation                      0x000000010ad8f016 -[NSInvocation invokeWithTarget:] + 54
    15  MyApp                            0x000000010520ea76 objc_msgSend_InjectionArguments + 454
    16  MyApp                            0x000000010520e83e -[TyphoonAssemblyDefinitionBuilder definitionForKey:] + 254
    17  MyApp                            0x000000010520e61f -[TyphoonAssemblyDefinitionBuilder populateCacheWithDefinitionForKey:] + 111
    18  MyApp                            0x000000010520e01b -[TyphoonAssemblyDefinitionBuilder builtDefinitionForKey:args:] + 203
    19  MyApp                            0x000000010520df20 -[TyphoonAssemblyDefinitionBuilder buildDefinitionForKey:] + 64
    20  MyApp                            0x000000010520de43 __49-[TyphoonAssemblyDefinitionBuilder populateCache]_block_invoke + 163
    21  CoreFoundation                      0x000000010ad6f0df __51-[__NSSetM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 79
    22  CoreFoundation                      0x000000010ad6efea -[__NSSetM enumerateObjectsWithOptions:usingBlock:] + 202
    23  MyApp                            0x000000010520dd69 -[TyphoonAssemblyDefinitionBuilder populateCache] + 185
    24  MyApp                            0x000000010520dc1a -[TyphoonAssemblyDefinitionBuilder builtDefinitions] + 74
    25  MyApp                            0x000000010520a976 -[TyphoonAssembly definitions] + 54
    26  MyApp                            0x000000010520ff1a -[TyphoonBlockComponentFactory registerAllDefinitions:] + 90
    27  MyApp                            0x000000010520fd78 -[TyphoonBlockComponentFactory buildAssembly:] + 104
    28  MyApp                            0x000000010520fc10 -[TyphoonBlockComponentFactory initWithAssemblies:] + 544
    29  MyApp                            0x000000010520f8b7 +[TyphoonBlockComponentFactory factoryWithAssemblies:] + 87
    30  MyApp                            0x00000001052340d2 +[TyphoonStartup requireInitialFactory] + 194
    31  MyApp                            0x00000001052345ad __60+[TyphoonStartup swizzleSetDelegateMethodOnApplicationClass]_block_invoke + 93
    32  UIKit                               0x0000000108a8825b -[UIApplication _setDelegate:assumeOwnership:] + 51
    33  UIKit                               0x0000000108a93cea _UIApplicationMainPreparations + 1618
    34  UIKit                               0x0000000108a93647 UIApplicationMain + 124
    35  MyApp                            0x0000000104b1f80f main + 111
    36  libdyld.dylib                       0x000000010c5a492d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:1)

Typhoon是一种相当典型的内省和反射型DI容器,针对Objective-C进行了优化。 (对Swift也很有效)。

  • 启动时:Typhoon从所有装配中收集有关如何构建对象的信息
  • 在运行时:汇编界面摆在TyphoonComponentFactory前面,它知道如何根据所有这些收集的规则发出组件。

获取组件:

Rules parsed on startup->post processing->factory emits built objects

因此:在运行时参数的情况下,我们不能在它们上调用任何方法,或者在传入之后操纵它们,因为当Typhoon构建对象实例化蓝图时,这与启动阶段无关。

如果声明类型为NSString的运行时参数,并且直接传递它而不进行操作(而不是NSString(string: storyboardIdentifier)),它应该可以正常工作。