台风 + Swift:使用 运行 时间参数时崩溃

Typhoon + Swift: Crash when using run-time arguments

我目前正在尝试使用 Typhoon 将依赖注入添加到我的应用程序中,现在一半 Swift,一半是遗留 Objective-C。我正在尝试从程序集中的故事板实例化视图控制器,但在下面的崩溃中 运行。我在这里 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

Typhoon是一个比较典型的内省反射型DI容器,针对Objective-C进行了优化。 (也适用于 Swift)。

  • 启动时:Typhoon 从所有程序集中收集有关如何构建对象的信息
  • 在运行时:程序集接口位于 TyphoonComponentFactory 前面,它知道如何根据所有这些收集到的规则发出组件。

获取组件:

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

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

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